hi. 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 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?