嵌套JSON的POJO格式? [英] Format of POJO for nested JSON?

查看:111
本文介绍了嵌套JSON的POJO格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [{data:{item1:value1,item2 :value2}}] 

当您获得值'value1'和'value2'时,必须首先访问数据?



如果这些字段位于根目录下,那么我可以让该方法返回带有这些字段名称的POJO。






我基本上想要下面的工作。

  @GET(/ path / to / data /)
Pojo getData();

class Pojo
{
public String item1;
public String item2;


解决方案

使用Gson库将你的json字符串转换为带有必要字段的Pojo对象。

  Gson gson = new Gson(); 

JsonArray jsonArray = gson.fromJson(jsonString,JsonElement.class).getAsJsonArray(); //将Json字符串转换为JsonArray
$ b $ j JsonObject jsonObj = jsonArray.get(0).getAsJsonObject(); //获取数组的第一个元素并将其转换为Json对象

Pojo pojo = gson.fromJson(jsonObj.get(data)。toString(),Pojo.class); //从json对象获取数据属性并将其转换为Pojo对象

或者你可以定义你的嵌套Pojo类来解析它。

  class Pojo 
{
private String item1;
private String item2;

// Setter和Getters
}

class数据
{
私有Pojo数据;

// Setter和Getters
}

ArrayList< Data> fromArray(jsonString,new TypeToken< List< Data>>(){}。getType());

编辑:尝试使用Retrofit获取value1和value2的代码。

  class Pojo 
{
private String item1;
private String item2;

// Setter和Getters
}

class数据
{
私有Pojo数据;

// Setter和Getters
}
$ b $ class MyData
{
private ArrayList< Data>数据列表;

// Setter and Getters
}

IService service = restAdapter.create(IService.class);
MyData data = service.getData();

ArrayList< Data> list = data.getDataList(); //从MyData返回arraylist

Data obj = list.get(0); //从数组列表中获取第一个元素

Pojo pojo = obj.getData(); //从数据获取pojo

Log.e(pojo,pojo.item1 +,+ pojo.item2);


So lets say the JSON response is:

[{ "data" : { "item1": value1, "item2:" value2 }}]

How do you get the values 'value1' and 'value2' when you must first access data?

If the fields were at the root then I could just have the method return a POJO with those field names.


I basically want the below to work.

@GET("/path/to/data/")
Pojo getData();

class Pojo
{
public String item1;
public String item2;
}

解决方案

You can try below code to convert your json string to Pojo object with required fields using Gson library.

Gson gson = new Gson();

JsonArray jsonArray = gson.fromJson (jsonString, JsonElement.class).getAsJsonArray(); // Convert the Json string to JsonArray

JsonObject jsonObj = jsonArray.get(0).getAsJsonObject(); //Get the first element of array and convert it to Json object

Pojo pojo = gson.fromJson(jsonObj.get("data").toString(), Pojo.class); //Get the data property from json object and convert it to Pojo object

or you can define your nested Pojo class to parse it.

class Pojo
{
    private String item1;
    private String item2;

    //Setters and Getters
}

class Data
{
    private Pojo data;

    //Setters and Getters
}

ArrayList<Data> yourArray = new Gson().fromJson(jsonString, new TypeToken<List<Data>>(){}.getType());

EDIT : Try below code to get value1 and value2 using Retrofit.

class Pojo
{
    private String item1;
    private String item2;

    //Setters and Getters
}

class Data
{
    private Pojo data;

    //Setters and Getters
}

class MyData
{
    private ArrayList<Data> dataList;

    //Setters and Getters
}

IService service = restAdapter.create(IService.class);
MyData data = service.getData(); 

ArrayList<Data> list = data.getDataList(); // Retrive arraylist from MyData

Data obj = list.get(0); // Get first element from arraylist

Pojo pojo = obj.getData(); // Get pojo from Data 

Log.e("pojo", pojo.item1 + ", " + pojo.item2);

这篇关于嵌套JSON的POJO格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆