java - Parse nested JSON with varying number of entries -
i'm working on bus transit app , 1 of network calls need make gets trip info on how point point b. bus transit api has method provides info. method returns 3 viable itineraries, , each itinerary has variable amount of legs. there 2 types of legs, walking legs , riding legs.
i wondering on how should go parsing data , transferring ui. what's throwing me off nested entries have nested arrays of variable length (that can of either 2 types). first retrieve data think using arraylist of arraylist work. should create 2 models correspond 2 type of legs, add data those, , dynamically add listview i'm going use display results? (i have dynamically add itineraries).
oh , thing. if bus need on finishes route , starts new 1 (while need stay on whole time) return multiple services in single leg , have no idea how efficiently check , convey user.
here's link documentation https://developer.cumtd.com/documentation/v2.2/method/getplannedtripsbystops
here's sample json response bus website gives
{ "time":"2012-01-26t13:22:05-06:00", "new_changeset": true, "status":{ "code":200, "msg":"ok" }, "rqst":{ "method":"getplannedtripsbystops", "params":{ "destination_stop_id":"unipspct", "origin_stop_id":"dncncltn" } }, "itineraries":[ { "start_time":"2012-01-26t13:41:00-06:00", "end_time":"2012-01-26t14:11:00-06:00", "travel_time":30, "legs":[ { "services":[ { "begin":{ "lat":40.122199, "lon":-88.295524, "name":"duncan & clayton (se corner)", "stop_id":"dncncltn:2", "time":"2012-01-26t13:41:00-06:00" }, "end":{ "lat":40.131976, "lon":-88.288742, "name":"parkland college", "stop_id":"pkln:1", "time":"2012-01-26t13:45:00-06:00" }, "route":{ "route_color":"725700", "route_id":"9a brown", "route_long_name":"brown", "route_short_name":"9a", "route_text_color":"ffffff" }, "trip":{ "trip_id":"9ba311__bb3", "trip_headsign":"counry fair / parkland", "route_id":"9a brown", "block_id":"bb3", "direction":"a", "service_id":"bb3", "shape_id":"9a" } }, { "begin":{ "lat":40.131976, "lon":-88.288742, "name":"parkland college", "stop_id":"pkln:1", "time":"2012-01-26t13:58:00-06:00" }, "end":{ "lat":40.116209, "lon":-88.257355, "name":"university & prospect (se far side)", "stop_id":"unipspct:6", "time":"2012-01-26t14:11:00-06:00" }, "route":{ "route_color":"666666", "route_id":"grey", "route_long_name":"grey", "route_short_name":"7", "route_text_color":"ffffff" }, "trip":{ "trip_id":"3gr763__bb3", "trip_headsign":"east - edgewood", "route_id":"grey", "block_id":"bb3", "direction":"east", "service_id":"bb3", "shape_id":"7e" } } ], "type":"service" } ] } ] }
any appreciated i've been thinking lot , haven't made real progress.
this have far (only have code parsing right now).
final string json_itineraries = "itenararies"; final string json_stop_name = "stop_name"; final string json_stop_lat = "stop_lat"; final string json_stop_long = "stop_long"; final string json_stop_id = "stop_id"; try { jsonarray itinerariesarray; jsonobject stopsjson = new jsonobject(jsonstr); itinerariesarray = stopsjson.getjsonarray(json_itineraries); arraylist itinerariesarraylist = new arraylist<arraylist<string>>(itinerariesarray.length()); (int = 0; < itinerariesarray.length(); i++) { jsonobject singletrip = itinerariesarray.getjsonobject(i); jsonarray singletriplegsjson = singletrip.getjsonarray("legs"); arraylist legsarraylist = new arraylist<string>(singletriplegsjson.length()); (int j=0; j<singletriplegsjson.length(); j++) { jsonobject singletripobjectsingleleg = singletriplegsjson.getjsonobject(j); string type = singletripobjectsingleleg.getstring("type") //????? }
i recommand use gson. gson very useful tool json.
first, use http://www.jsonschema2pojo.org/ make class json.
copy & paste json string site,
select "source type : json",
select "annotation style: gson",
press "preview" or "jar" java classes.
second, use gson parse json string. visit here more examples.
example:
private gson gson = new gson();
string sampledata = // "[" // + "{\"info\": {\"place\": \"place1\"}," + "\"events\": {\"info\": \"555f1fc297f229004dd6e8aa\",\"time\": \"5\",\"image\": \"555f1fc2d197270b6c732d3b\",\"event_name\": \"555f1fc224d1cb629a8ef603\"}}," + "{\"info\": {\"place\": \"place3\"}," + "\"events\": {\"info\": \"555f1fc283c7b150ede89c05\",\"time\": \"7\",\"image\": \"555f1fc2bf5fa8a3b320e0ca\",\"event_name\": \"555f1fc20d40f1b478610505\"}}," + "{\"info\": {\"place\": \"place2\"}," + "\"events\": {\"info\": \"555f1fc29163e85ae42e7518\",\"time\": \"6\",\"image\": \"555f1fc21506a186c2d34a92\",\"event_name\": \"555f1fc272a06e68b8c3f4b7\"}}" + " ]"; java.lang.reflect.type listtype = new typetoken<list<event>>() { }.gettype(); arraylist<event> eventlist = gson.fromjson(sampledata, listtype);
Comments
Post a Comment