我需要使用 android json 解析器在 android 中解析这个响应,但我无法在任何地方找到答案是:
如果例如“行程”可以包含一个或有时多个行程类型的对象,我该如何解析数据?如果它包含一个而不是像这样返回,但如果它包含更多,则返回 [] 在此示例中,“itinerary”不能放入 JsonArray 中,因为它显然不是数组。(没有放在[]中对吗?)
我该如何解析这个?有什么例子吗?
{
"plan":{
"date":"2010-10-20T00:00:00+02:00",
"from":{
"name":"Булевар Партизански Одреди",
"stopId":"123",
"lon":"21.373255285035548",
"lat":"42.00736515785779",
"geometry":"{\"type\": \"Point\", \"coordinates\": [21.373255285035548,42.00736515785779]}"
},
"to":{
"name":"Булевар Партизански Одреди",
"stopId":"123",
"lon":"21.37228809181389",
"lat":"42.00762790595865",
"geometry":"{\"type\": \"Point\", \"coordinates\": [21.37228809181389,42.00762790595865]}"
},
"itineraries":{
"itinerary":{
"duration":"159000",
"startTime":"2010-10-20T00:00:00+02:00",
"endTime":"2010-10-20T00:02:39+02:00",
"walkTime":"159000",
"transitTime":"0",
"waitingTime":"0",
"walkDistance":"212.6496008849819",
"elevationLost":"0.0",
"elevationGained":"0.0",
"transfers":"0",
"legs":{
"leg":{
"@route":"Булевар Партизански Одреди",
"@mode":"WALK",
"startTime":"2010-10-20T00:00:00+02:00",
"endTime":"2010-10-20T00:02:39+02:00",
"distance":"212.6496008849819",
"from":{
"name":"Булевар Партизански Одреди",
"lon":"21.373255285035548",
"lat":"42.00736515785779",
"geometry":"{\"type\": \"Point\", \"coordinates\": [21.373255285035548,42.00736515785779]}"
},
"to":{
"name":"Булевар Партизански Одреди",
"lon":"21.37228809181389",
"lat":"42.00762790595865",
"geometry":"{\"type\": \"Point\", \"coordinates\": [21.37228809181389,42.00762790595865]}"
},
"legGeometry":{
"length":"3",
"points":"_qk_GymmaCf@qC{ArI"
},
"steps":{
"walkSteps":{
"distance":"212.6496008849819",
"streetName":"Булевар Партизански Одреди",
"absoluteDirection":"EAST",
"stayOn":"false",
"becomes":"false",
"lon":"21.373255285035548",
"lat":"42.00736515785779",
"elevation":""
}
},
"duration":"159000"
}
},
"tooSloped":"false"
}
}
},
"requestParameters":{
"entry":[
{
"key":"optimize",
"value":"QUICK"
},
{
"key":"time",
"value":"9:40 am\""
},
{
"key":"wheelchair",
"value":"false"
},
{
"key":"maxWalkDistance",
"value":"800.0"
},
{
"key":"fromPlace",
"value":"42.0074711701039,21.3732840843651"
},
{
"key":"toPlace",
"value":"42.0076745404488,21.3723007605583"
},
{
"key":"date",
"value":"10/20/2010"
},
{
"key":"mode",
"value":"TraverseMode (WALK, TRAM, SUBWAY, RAIL, BUS, FERRY, CABLE_CAR, GONDOLA, FUNICULAR, TRANSIT, TRAINISH, BUSISH)"
},
{
"key":"numItineraries",
"value":"3"
}
]
}
}
这是我用于第一部分的内容
JSONObject planObject=json.getJSONObject("plan");
Log.i("date",planObject.get("date").toString());
JSONObject fromObject=planObject.getJSONObject("from");
Log.i("from object",fromObject.get("name").toString());
Log.i("from object",fromObject.get("stopId").toString());
Log.i("from object",fromObject.get("lon").toString());
Log.i("from object",fromObject.get("lat").toString());
这是菲利克斯写的例子,它是关于多个“行程”
"itineraries": [
{"duration": "123456", ... },
{"duration": "789012", ... }
]
这将是相同的示例,但仅针对一个示例:
"itineraries":
{"duration": "123456", ... },
所以在第二种情况下没有 JSONArray 所以如果我尝试使用 Felix 提供的解析数组的代码,它将返回一个错误。
所以问题是:检查值是否可以放入 JSONArray 的方法是什么。是使用命令 optJSONArray("possibleArrayValues")!=null 还是有更好的方法然后进行大量 if-then 检查?