android - Gson Parse Json with array with different object types -



android - Gson Parse Json with array with different object types -

how can parse json using gson? have array multiple object types , don't know kind of object need create save structure. cannot alter json message (i don't command server).

the class function (sort of) this

public class response { private list<object> tr; private int results; (...) }

json message (note array multiple object types.)

{ "tr": [ { "a": { "userid": "112" } }, { "b": { "userid": "123", "address":"street dummy" } }, { "a": { "userid": "154" } } ], "results":3 }

the gson user's guide explicitly covers this:

https://sites.google.com/site/gson/gson-user-guide#toc-serializing-and-deserializing-collection-with-objects-of-arbitrary-types

you have object field tr array containing arbitrary types.

the users guide explains can't straight deserialize such structure, , recomends:

use gson's parser api (low-level streaming parser or dom parser jsonparser) parse array elements , utilize gson.fromjson() on each of array elements. preferred approach.

in case ... depend on objects possible in array. if going have same inner object you'd want like...

list<myuserpojo> list = new arraylist<myuserpojo>(); jsonarray array = parser.parse(json).getasjsonobject().getasjsonarray("tr"); (jsonelement je : array) { set<map.entry<string,jsonelement>> set = je.getasobject().entryset(); jsonelement je2 = set.iterator().next().getvalue(); myuserpojo mup = new gson().fromjson(je2, myuserpojo.class); list.add(mup); }

and of course, need within custom deserializer actual object have tr , results fields.

class mypojo { list<myuserpojo> userlist; int results; } class myuserpojo { string userid; string address; } class mydeserializer implements jsondeserializer<mypojo> { @override public mypojo deserialize(jsonelement je, type type, jsondeserializationcontext jdc) throws jsonparseexception { list<myuserpojo> list = new arraylist<myuserpojo>(); jsonarray array = je.getasjsonobject().getasjsonarray("tr"); (jsonelement je2 : array) { set<map.entry<string,jsonelement>> set = je2.getasobject().entryset(); jsonelement je3 = set.iterator().next().getvalue(); myuserpojo mup = new gson().fromjson(je3, myuserpojo.class); list.add(mup); } mypojo mp = new mypojo(); mp.tr = list; mp.results = je.getasobject().getasjsonprimitive("results").getasint(); homecoming mp; } }

now you're set - can utilize deserializer , create object:

gson gson = new gsonbuilder() .registertypeadapter(mypojo.class, new mydeserializer()) .build(); mypojo mp = gson.fromjson(json, mypojo.class);

if a, b etc of import ... well, you'll have figure out. above should on way understanding what's going needed deal json structure.

for completeness sake, "hacky" way around if there limited number of types , inner object limited in terms of fields. create pojo encompasses possibilities:

class mypojo { mysecondpojo a; mysecondpojo b; ... mysecondpojo f; } class mysecondpojo { string userid; string address; ... string someotherfield; }

when gson deserializes json set missing fields in pojo(s) null. have tr list or array of these in pojo. 1 time again , emphasize, this quite hacky , wrong way it, thought i'd explain required straight parse array.

android json gson

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -