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

10-04 17:14