亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

卡住解析嵌套的 JSON 數組

卡住解析嵌套的 JSON 數組

開滿天機 2022-05-25 16:25:59
我一直在解析嵌套的 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);


查看完整回答
反對 回復 2022-05-25
  • 1 回答
  • 0 關注
  • 69 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號