簡體   English   中英

卡住解析嵌套的JSON數組

[英]Stuck Parsing nested JSON Array

我在解析嵌套的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);
        }

我的StoreMobileNumbersClass:

   public class StoreMobileNumber{

   public Long id;
   public String mobile;

   }

到目前為止,我的努力一直沒有成功。 我收到以下錯誤:

   org.json.JSONException: No value for storeMobileNumbers

有人可以幫我看看我在這里想念的東西嗎?

更改為-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM