[英]Stuck Parsing nested JSON Array
I'm stuck parsing a nested JSON Array. 我在解析嵌套的JSON数组。 This should be fairly simple but I can't seem to get it to work. 这应该很简单,但是我似乎无法正常工作。
I have a JSON Array like below: 我有一个如下的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
}]
}]
}
I'm trying to get the address from the list first, then get the values from storeMobileNumbers. 我想先从列表中获取地址,然后从storeMobileNumbers中获取值。
I have a POJO class for the list, and now I have created a POJO class for the StoreMobileNumbers. 我有一个用于列表的POJO类,现在我为StoreMobileNumbers创建了一个POJO类。
This is what I have right now: 这就是我现在所拥有的:
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: 我的StoreMobileNumbersClass:
public class StoreMobileNumber{
public Long id;
public String mobile;
}
So far, my efforts have been unsuccessful. 到目前为止,我的努力一直没有成功。 I'm getting the following error: 我收到以下错误:
org.json.JSONException: No value for storeMobileNumbers
Can someone help me see what I'm missing here? 有人可以帮我看看我在这里想念的东西吗?
Change to following - 更改为-
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.