【问题标题】:how to get Json objects from json array and use them with model class如何从 json 数组中获取 Json 对象并将它们与模型类一起使用
【发布时间】:2018-07-20 07:05:35
【问题描述】:

我想使用 gson 库将收到的 json 数据链接到我的 pojo 类。我使用 volley 库来接收数据。我应该怎么做,以便每当我从 pojo 类调用 getter 方法时,我都会收到收到的 json 数据.

我的 Json 数据就是这种格式。

{
 "vichList":[ {
            id=1,
            username="abc....},
            {....},
            ]
}

我想把这个 json 数据放到我的 pojo 类中。

Vich.java

public class GetfeedResponse {
private List<Vich> vichList;

public List<Vich> getVichList() {
    return vichList;
}

public void setVichList(List<Vich> vichList) {
    this.vichList = vichList;
}
}

Vich.java

public class Vich {
private int id;
private String username;
private String full_name;
private String createdAt;
private int vich_id;
private String vich_content;
private String city;
private int like_count;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getFull_name() {
    return full_name;
}

public void setFull_name(String full_name) {
    this.full_name = full_name;
}

public String getCreatedAt() {
    return createdAt;
}

public void setCreatedAt(String createdAt) {
    this.createdAt = createdAt;
}

public int getVich_id() {
    return vich_id;
}

public void setVich_id(int vich_id) {
    this.vich_id = vich_id;
}

public String getVich_content() {
    return vich_content;
}

public void setVich_content(String vich_content) {
    this.vich_content = vich_content;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public int getLike_count() {
    return like_count;
}

public void setLike_count(int like_count) {
    this.like_count = like_count;
}
}

这里我正在使用 volley 库获取 json 响应。

httpUtil.getrequest(url,this,new VolleyCallback(){

        @Override
        public void onSuccess(String result){

            GetfeedResponse getfeedResponse = new GetfeedResponse();
           // for(Vich vich : getfeedResponse.getVichList()){
           // }

            Log.d("Response Result:",result);
        }

如何从 json 数组中获取对象并在 pojo 类的帮助下使用它们?

【问题讨论】:

  • 请使用jackson 2 api

标签: java android json android-json


【解决方案1】:

使用 Gson

在您的 gradle 中添加以下依赖项:

implementation 'com.google.code.gson:gson:2.8.5'

在你的onSuccess()

GetfeedResponse getfeedResponse=new Gson().fromJson(result, GetfeedResponse.class);

如果您希望使用 Volley 和 POJO,最好使用自定义 GSON 请求。检查此链接:Custom GSON request With Volley

【讨论】:

    【解决方案2】:

    GSON:

    GetfeedResponse parsed = new Gson().fromJson(response, GetfeedResponse.class);
    

    杰克逊:

    GetfeedResponse parsed = new ObjectMapper().readValue(response, GetfeedResponse.class);
    

    此外,如果您只想转换 Vich 项目列表(并且相应地剥离了 JSON),您可以执行以下操作:

    [ {
    id=1,
    username="abc....},
    {....},
    ]
    
    List<Vich> viches = Arrays.asList(new Gson().fromJson(vichItemsJson, Vich[].class));
    

    【讨论】:

    • 我怎样才能在 List viches = Arrays.asList(new Gson().fromJson(vichItemsJson, Vich[].class)) 中得到 vichItemsJson; ?
    • 呃,你为什么要这么做?我只是提到它作为仅供参考,这样的事情是可能的。您已经有了 POJO 类 - 使用它比手动将每条响应手动转换为所需的对象要好得多。只需按照建议执行GetfeedResponse parsed = new Gson().fromJson(response, GetfeedResponse.class);,然后您就可以使用List&lt;Vich&gt; viches = parsed.getVichList();获取Vich列表
    猜你喜欢
    • 2018-03-14
    • 2016-12-17
    • 2019-06-14
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多