简体   繁体   English

如何从json数组获取Json对象并将其与模型类一起使用

[英]how to get Json objects from json array and use them with model class

I want to link my received json data to my pojo class using gson library.I used volley library to receive the data.What should i do so that whenever i call getter methods from my pojo class then i get the received json data. 我想使用gson库将接收到的json数据链接到我的pojo类。我使用了volley库来接收数据。当我从pojo类中调用getter方法时,我应该怎么做才能获取接收到的json数据。

My Json data is in this format. 我的Json数据是这种格式。

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

I want to get this json data into my pojo class. 我想将此json数据放入我的pojo类中。

Vich.java 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 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;
}
}

Here i am getting the json response using volley library. 在这里,我得到了使用凌空库的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);
        }

How can i get objects from json array and use them with the help of pojo class? 我如何从json数组中获取对象并在pojo类的帮助下使用它们?

Using Gson 使用Gson

Add the following dependency in your gradle: 在gradle中添加以下依赖项:

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

In your onSuccess() 在你的onSuccess()

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

If you wish to use Volley and POJO its better to use custom GSON request. 如果您想使用Volley和POJO,最好使用自定义GSON请求。 Check this link : Custom GSON request With Volley 检查此链接: 排球的定制GSON请求

GSON: GSON:

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

Jackson: 杰克逊:

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

Additionally, if you wanted to convert only list of Vich items (and you stripped your JSON accordingly) you could do following: 此外,如果您只想转换Vich项目列表(并且相应地剥离了JSON),则可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从JSON数组中获取随机对象 - How to get random objects from a JSON array 如果我想从 json 文件与 Z67166080962DE308CD762A 一起读取配对对象,我应该使用什么 java class? - What java class should I use for pair objects if I want to read them in from a json file with gson? 在Apache Jena中将Json-Ld对象的数组读入模型中。如何从模型中检索单个对象? - Read array of Json-Ld Objects into a Model In Apache Jena.How to retrieve individual objects from Model? 如何使用GSON从JSON响应中获取对象数组 - How to get array of objects from JSON response using GSON 从json字符串中提取对象数组,并将其放入ArrayList中? - Extract an array of objects from json string and put them in ArrayList? 如何解析所有 json 对象并将它们发送到一个类中? - How to parse all json objects and send them inside a class? 如何从包含未经修改的数组的String JSON获取Java中的JSON对象的String []数组 - How to get String[] array of JSON objects in java from String JSON that is contained an array without modification java + json:如何从包含对象数组{[{},{}]}的json对象中获取元素 - java+json: how to get an element from a json object that contains an array of objects {[ {},{} ]} 如何在JSON数组的嵌套JSON对象中获取一些元素 - How to get some elements in nested JSON objects in a JSON array 如何从php获取json数组并在android中使用其值? - how to get a json array from php and use its value in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM