当前位置:   article > 正文

java json 嵌套list_java-嵌套JSON的POJO格式?

jsonarray jsonarray = gson.fromjson(notice.getnocviewuser(), jsonarray.class

您可以尝试以下代码,使用Gson库将JSON字符串转换为具有必填字段的Pojo对象.

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

或者,您可以定义嵌套的Pojo类来解析它.

class Pojo

{

private String item1;

private String item2;

//Setters and Getters

}

class Data

{

private Pojo data;

//Setters and Getters

}

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

编辑:尝试下面的代码以使用翻新获取value1和value2.

class Pojo

{

private String item1;

private String item2;

//Setters and Getters

}

class Data

{

private Pojo data;

//Setters and Getters

}

class MyData

{

private ArrayList dataList;

//Setters and Getters

}

IService service = restAdapter.create(IService.class);

MyData data = service.getData();

ArrayList 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);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/618588
推荐阅读
相关标签
  

闽ICP备14008679号