我一直在解析嵌套的 JSON 数组。这应该相当简单,但我似乎无法让它工作。我有一个如下的 JSON 数组: {"currentPage":0, "totalPages":1, "totalSize":1, "first":true, "last":true, "list"[{"date":"2018-11-07T09:34:25.042+0000", "lastUpdated":"2018-11-07T09:34:25.266+0000", "id"130674, "active":true, "address":null, "storeMobileNumbers": [{"id":130676,"mobile":"+201008005090","version":0 }] }]}我试图先从列表中获取地址,然后从 storeMobileNumbers 中获取值。我有一个用于列表的 POJO 类,现在我为 StoreMobileNumbers 创建了一个 POJO 类。这就是我现在所拥有的: Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); Type collectionType = new TypeToken<List<MyStore>>(){}.getType(); Type collection2 = new TypeToken<List<StoreMobileNumber>>({}.getType(); JSONObject obj = new JSONObject(json); if (obj.has("list")) { String myList = obj.get("list").toString(); list1 = gson.fromJson(myList, collectionType); JSONArray obj1 = new JSONArray(list1); JSONObject numbersObject = obj1.toJSONObject(obj1); String mobileList = numbersObject.get("storeMobileNumbers").toString(); mobileNumbers = gson.fromJson(mobileList, collection2); }My StoreMobileNumbersClass: public class StoreMobileNumber{ public Long id; public String mobile; }到目前为止,我的努力没有成功。我收到以下错误: org.json.JSONException: No value for storeMobileNumbers有人可以帮我看看我在这里缺少什么吗?
1 回答
蓝山帝景
TA贡献1843条经验 获得超7个赞
更改为以下 -
String myList = obj.get("list").toString();
list1 = gson.fromJson(myList, collectionType); // delete this not required
JSONArray obj1 = new JSONArray(list1);
JSONObject numbersObject = obj1.getJsonObject(0); // change this to get first object from the JsonArray
String mobileList =
numbersObject.get("storeMobileNumbers").toString();
mobileNumbers = gson.fromJson(mobileList, collection2);
添加回答
举报
0/150
提交
取消