hello i'm trying parse json file link.
the returned json follows-
i need loop through instances of journeys first, have loop through legs each journeys detailed instruction. can see each legs consists of instruction in parts returns string, ultimate goal combine these string each journeys , display them textview. above json end goal display -
jubilee line towards stratford, or north greenwich
northern line towards edgware, or high barnet
till i've been trying navigate through json without luck.
here code i've been working on-
try { //object json file. jsonarray journey = object.getjsonarray("journeys"); if (journey != null) { //retrieving number of possible routes. (int i=0;i<journey.length();i++){ routes.add(journey.getjsonobject(i)); } //retrieving number of possible legs each route. if (!routes.isempty()){ (int j = 0; j< routes.size(); j++){ legs.add(j, routes.get(j).getjsonarray("legs")); } //trying retrieve detailed instruction here , failing. for(int k=0;k<routes.get(k).getjsonarray("legs").length();k++){ instructiondetail.add(k,legs.get(k).getjsonobject(k).getjsonobject("instruction")); } } } } catch (jsonexception e) { e.printstacktrace(); } i believe approach wrong , didn't loop right.. suggestions parse , navigate , other approach highly appreciated!
thanks!
update:
jsonarray journeys = new jsonobject(""); for(int = 0 ; < journeys.length() ; i++) { // traverse journeys jsonobject journey = journeys.getjsonobject(i); // journeys(i) -> journey if(journey.has("legs")) { // if journey has "legs" key jsonarray legs = journey.getjsonarray("legs"); // legs array journey object for(int j = 0 ; j < legs.length() ; j++) { // traverse legs jsonobject leg = legs.getjsonobject(j); // legs(j) -> leg if(leg.has("instruction")) { // if leg has "instruction" key in jsonobject instruction = leg.getjsonobject("instruction"); // instruction jsonobject string detailed = instruction.optstring("detailed", "fallback detailed"); // detailed string in instruction object } } } } update:
private static class detail { string journeytype; string legtype; string instructiontype; string detail; public detail(string journeytype, string legtype, string instructiontype, string detail) { this.journeytype = journeytype; this.legtype = legtype; this.instructiontype = instructiontype; this.detail = detail; } } ... ...
list<detail> detaillist = new arraylist<>(); jsonarray journeys = new jsonobject(""); for(int = 0 ; < journeys.length() ; i++) { // traverse journeys jsonobject journey = journeys.getjsonobject(i); // journeys(i) -> journey if(journey.has("legs")) { // if journey has "legs" key jsonarray legs = journey.getjsonarray("legs"); // legs array journey object for(int j = 0 ; j < legs.length() ; j++) { // traverse legs jsonobject leg = legs.getjsonobject(j); // legs(j) -> leg if(leg.has("instruction")) { // if leg has "instruction" key in jsonobject instruction = leg.getjsonobject("instruction"); // instruction jsonobject string journeytype = journey.getstring("$type"); string legtype = leg.getstring("$type"); string instructiontype = instruction.getstring("$type"); string detailed = instruction.getstring("detailed"); // detailed string in instruction object detaillist.add(new detail(journeytype, legtype, instructiontype, detailed)); } } } } for(detail detail : detaillist) { textview textview = new textview([yourcontext]); textview.settext(detail.detail); yourcontentviewgroup.addview(textview); // or can use view.inflate(context, layoutres, yourcontentviewgroup) , design layout show other detail instance values } 


Comments
Post a Comment