我可以从外部JSON检索对象,但我无法从嵌套的JSON中检索数据。
如何检索自定义对象的每个ArrayList,然后从嵌套JSON的自定义对象的ArrayList中检索每个对象?
我曾尝试使用解析器库Jackson Json,但我是Android新手,因为该项目使用多个由链接类组成的ArrayList,使用Jackson的自定义对象意味着对我的HTTPCall类和各种相关的内部处理程序类进行了许多复杂的更改。
我已经包含了尝试解析的JSON以及我尝试访问嵌套JSON的类。
但是当仅解析空括号时:[]或数组位置被返回:appName.classNameArrayName@ArraylocationInNumbers33129726498
JSON
[
{
"date": "20121212",
"summary": [
{
"activity": "walking",
"group": "walking",
"duration": 3333,
"distance": 3333,
"steps": 3333,
"calories": 300
},
{
"activity": "walking_on_treadmill",
"group": "walking",
"duration": 270,
"steps": 303,
"calories": 30
},
{
"activity": "transport",
"group": "transport",
"duration": 1124,
"distance": 8443
},
{
"activity": "underground",
"group": "transport",
"duration": 1003,
"distance": 8058
},
{
"activity": "zumba",
"duration": 570,
"calories": 200
}
],
"segments": [
{
"type": "place",
"startTime": "20121212T000000+0200",
"endTime": "20121212T071430+0200",
"place": {
"id": 1,
"type": "unknown",
"location": {
"lat": 55.55555,
"lon": 33.33333
}
},
"lastUpdate": "20130317T121143Z"
},
{
"type": "move",
"startTime": "20121212T071430+0200",
"endTime": "20121212T074617+0200",
"activities": [
{
"activity": "walking",
"group": "walking",
"manual": false,
"startTime": "20121212T071430+0200",
"endTime": "20121212T072732+0200",
"duration": 782,
"distance": 1251,
"steps": 1353,
"calories": 99,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T071430+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T072732+0200"
}
]
},
{
"activity": "transport",
"group": "transport",
"manual": false,
"startTime": "20121212T072732+0200",
"endTime": "20121212T074616+0200",
"duration": 1124,
"distance": 8443,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T072732+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T074208+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T074617+0200"
}
]
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "place",
"startTime": "20121212T074617+0200",
"endTime": "20121212T100051+0200",
"place": {
"id": 2,
"type": "unknown",
"location": {
"lat": 55.55555,
"lon": 33.33333
}
},
"activities": [
{
"activity": "walking_on_treadmill",
"group": "walking",
"manual": true,
"duration": 270,
"steps": 303,
"calories": 30,
"trackPoints": []
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "move",
"startTime": "20121212T100051+0200",
"endTime": "20121212T100715+0200",
"activities": [
{
"activity": "walking",
"group": "walking",
"manual": false,
"startTime": "20121212T100051+0200",
"endTime": "20121212T100715+0200",
"duration": 384,
"distance": 421,
"steps": 488,
"calories": 99,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T100051+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T100715+0200"
}
]
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "place",
"startTime": "20121212T100715+0200",
"endTime": "20121212T110530+0200",
"place": {
"id": 4,
"name": "test",
"type": "foursquare",
"foursquareId": "4df0fdb17d8ba370a011d24c",
"foursquareCategoryIds": ["4bf58dd8d48988d125941735"],
"location": {
"lat": 55.55555,
"lon": 33.33333
}
},
"activities": [
{
"activity": "walking",
"group": "walking",
"manual": false,
"startTime": "20121212T101215+0200",
"endTime": "20121212T101255+0200",
"duration": 40,
"distance": 18,
"steps": 37,
"calories": 99,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T101215+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T101255+0200"
}
]
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "move",
"startTime": "20121212T110530+0200",
"endTime": "20121212T111129+0200",
"activities": [
{
"activity": "walking",
"group": "walking",
"manual": false,
"startTime": "20121212T110530+0200",
"endTime": "20121212T111128+0200",
"duration": 358,
"distance": 493,
"steps": 441,
"calories": 99,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T110531+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T110536+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T110947+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T111017+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T111129+0200"
}
]
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "place",
"startTime": "20121212T111129+0200",
"endTime": "20121212T153638+0200",
"place": {
"id": 2,
"type": "unknown",
"location": {
"lat": 55.55555,
"lon": 33.33333
}
},
"activities": [
{
"activity": "zumba",
"manual": true,
"duration": 570,
"calories": 200,
"trackPoints": []
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "move",
"startTime": "20121212T153638+0200",
"endTime": "20121212T160744+0200",
"activities": [
{
"activity": "underground",
"group": "transport",
"manual": false,
"startTime": "20121212T153638+0200",
"endTime": "20121212T155321+0200",
"duration": 1003,
"distance": 8058,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T153638+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T155322+0200"
}
]
},
{
"activity": "walking",
"group": "walking",
"manual": false,
"startTime": "20121212T155322+0200",
"endTime": "20121212T160744+0200",
"duration": 862,
"distance": 1086,
"steps": 1257,
"calories": 99,
"trackPoints": [
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T155322+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T155402+0200"
},
{
"lat": 55.55555,
"lon": 33.33333,
"time": "20121212T160744+0200"
}
]
}
],
"lastUpdate": "20130317T121143Z"
},
{
"type": "place",
"startTime": "20121212T160744+0200",
"endTime": "20121212T232730+0200",
"place": {
"id": 1,
"type": "unknown",
"location": {
"lat": 55.55555,
"lon": 33.33333
}
},
"lastUpdate": "20130317T121143Z"
}
],
"caloriesIdle": 1785,
"lastUpdate": "20130317T121143Z"
}
]
StorylineData (我尝试解析JSON的类。链接到提供解析数据的其他类,用于从以前解析的JSON创建自定义对象的ArrayLists)
/**
* Parse a {@link org.json.JSONObject} from storyline {@link org.json.JSONArray}, then return the corresponding {@link StorylineData} object.
*
* @param jsonObject : the storyline JSON object received from server
* @return corresponding {@link StorylineData}
*/
public static StorylineData parse(JSONObject jsonObject) throws JSONException {
if (jsonObject != null) {
StorylineData storylineData = new StorylineData();
storylineData.date = jsonObject.optString("date");
storylineData.caloriesIdle = jsonObject.optInt("caloriesIdle");
storylineData.lastUpdate = jsonObject.optString("lastUpdate");
storylineData.summary = new ArrayList<>();
storylineData.segments = new ArrayList<>();
JSONArray summariesJsonArray = jsonObject.optJSONArray("summary");
if (summariesJsonArray != null)
for (int i = 0; i < summariesJsonArray.length(); i++) {
JSONObject summaryJsonObject = summariesJsonArray.optJSONObject(i);
if (summaryJsonObject != null) {
summariesJsonArray.getJSONObject(i).getString("distance");
String group = summaryJsonObject.optString("group");
Integer distance = summaryJsonObject.optInt("distance");
storylineData.setDistance(distance);
...
storylineData.summary.add(SummaryData.parse(summaryJsonObject));
Log.d("StorylineDataCls \t sJo", summaryJsonObject.toString() + "Log\n");
System.out.println("print distance" + summariesJsonArray.getJSONObject(i).getString("distance"));
System.out.println("print summary" + summaryJsonObject);
}
}
JSONArray segmentsJsonArray = jsonObject.optJSONArray("segments");
if (segmentsJsonArray != null) {
for (int i = 0; i < segmentsJsonArray.length(); i++) {
JSONObject segment = segmentsJsonArray.optJSONObject(i);
if (segment != null) {
JSONArray activitiesJsonArray = segment.optJSONArray("activities");
if (activitiesJsonArray!=null) {
for (int j = 0; j < activitiesJsonArray.length(); j++) {
JSONObject trackPoints = activitiesJsonArray.optJSONObject(j);
if(trackPoints != null){
...
storylineData.trackPoints.add(TrackPointsData.parse(trackPoints));
}
}
}
..
Log.d("StorylineDataCls \t sSo", segment.toString());
System.out.println("print segment" + segment);
//System.out.println("print segments" + segments);
System.out.println("print segmentsJsonArray" + segmentsJsonArray);
}
}
}
return storylineData;
}
return null;
}