我一直在解析嵌套的 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
提交
取消