How to access deep JSON element in Java -


i have following json response:

{    "destination_addresses" : [ "berlin, deutschland" ],    "origin_addresses" : [ "mannheim, deutschland" ],    "rows" : [       {          "elements" : [             {                "distance" : {                   "text" : "624 km",                   "value" : 624195                },                "duration" : {                   "text" : "5 stunden, 53 minuten",                   "value" : 21156                },                "status" : "ok"             }          ]       }    ],    "status" : "ok" } 

my question is: how access "text" element in "distance" section?

i have following right now:

jsonobject object = (jsonobject) new jsontokener(response).nextvalue(); jsonarray rows= null; rows= object.getjsonarray("rows"); 

you either have continue parsing jsonarray, e.g.:

for(int = 0; < rows.length; i++) {     jsonarray elements = rows.getjsonarray(i);     for(int j = 0; j < elements.length; j++) {            jsonobject current = elements.getjsonobject(j);         string curtext = current.getjsonobject("distance").getjsonobject("text");     }     }  

or can represent json in pojo , deserialize using jackson's objectmapper example.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -