android - java how to extract json element for many depth levels -


if have json

{ "aggregations" : { "cityagg": { "'buckets": { "buckets" : [ {},{},{} ] } } } } 

i can extract last buckets array using simple json

jsonobject aggregations = (jsonobject)jsonresponse.get("aggregations");         jsonobject cityagg = (jsonobject)aggregations.get("cityagg");         jsonobject buckets = (jsonobject) cityagg.get("buckets");         jsonarray result = (jsonarray) buckets.get("buckets"); 

but boring make object each json level, isn't there better/easier way that?

something maybe like:

jsonresponse.get("aggregation/cityagg/buckets/buckets") 

you can use gson provided google format json,convert object json , vice versa.

gson java library can used convert java objects json representation. can used convert json string equivalent java object. gson can work arbitrary java objects including pre-existing objects not have source-code of.

can go through samples here : gson usage

example

{

"aggregations" : { "cityagg": { "'buckets": { "buckets" : [ {},{},{} ] } } } } 

create model has such depth hierarchy

class aggregations{   private cityagg cityagg;   //getters , setters }  class cityagg{  private bucket buckets;  //getters , setters }  class bucket{   //some properties } 

you'll use parent class properties loaded inside it


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -