json parse response can have one or multiple objects
i need to parse this response in android using the android json parser but the thing i cant find the answer to anywhere is:
how do i parse the data if for example "itineraries" can contain one or sometimes more objects of the type itinerary? if it contains one than it is returned like this but if it contains more it is returned with [] with this example "itinerary" cannot be placed into a JsonArray becouse obviously it is not an array. (not placed in [] right?)
how do i parse this? any examples?
{
"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"
}
]
}
}
here is what i use for the first part
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开发者_如何学Python object",fromObject.get("lon").toString());
Log.i("from object",fromObject.get("lat").toString());
this is the example Felix wrote and it is about multiple "itineraries"
"itineraries": [
{"duration": "123456", ... },
{"duration": "789012", ... }
]
this would be the same example but for one:
"itineraries":
{"duration": "123456", ... },
so in the second case there is no JSONArray so if i try to use the code Felix gave for parsing array it will return a error.
so the question is: what is the way to check if the value can be put in a JSONArray. is The command optJSONArray("possibleArrayValues")!=null used or is there a better method then doing lots of if-then checks?
I don't think it's legal to have multiple keys with the same name in a JSON object. I.e. the keys in a JSON object should form a set, not a list.
Can you post an example when there are more itineraries? I bet it would look something like:
...
"itineraries": [
{"duration": "123456", ... },
{"duration": "789012", ... }
]
...
If it does look like this, parsing it is easy:
JSONArray itineraries = planObject.getJSONArray("itineraries");
for (int i=0; i < itineraries.length(); i++) {
Log.i("TAG", itineraries.getJSONObject(i).getString("duration");
}
If it doesn't, the API you're working with is broken. Either fix it or tell whoever runs it to fix it :)
Edit: now that we know how a multiple-item response looks like, here's how to parse it:
Object itineraries = planObject.get("itineraries");
if (itineraries instanceof JSONObject) {
JSONObject itinerary = (JSONObject) itineraries;
// right now, itinerary is your single item
}
else {
JSONArray array = (JSONArray) itineraries;
// do whatever you want with the array of itineraries
}
Untested, but it should work.
精彩评论