android - Manually parse a portion of dynamic JSON object alongside Gson deserializer -
i want parse following json response dynamic json object,
{ "id": 1, "last_login": "2016-07-16t12:46:29.621996z", "point_of_sales": [ { "counter 4": { "type": "retail", "id": 6 } }, { "counter 5": { "type": "retail", "id": 7 } }, { "counter 6": { "type": "retail", "id": 8 } } ] }
here objects name inside "point_of_sales" array dynamic makes difficult parse using gson. here's i've tried far,
@serializedname("id") @expose private integer id; @serializedname("last_login") @expose private string lastlogin; @serializedname("point_of_sales") @expose private list<map<string, counter>> pointofsales = new arraylist<map<string, counter>>(); ....... getter & setters
counter class,
@serializedname("type") @expose private string type; @serializedname("id") @expose private integer id; ....... getter & setters
by following procedure can convert objects can't extract them map pojo classes.
(map<string, counter> objectmap : response.getpointofsales()) { // how extract counter pojo data map list? }
i tried way nothing works,
arraylist<counter> pojos = new arraylist<counter>(); try { (map<string, counter> objectmap : result.getpointofsales()) { counter pojo = new counter(); (map.entry<string, counter> property : objectmap.entryset()) { method setter = counter.class.getmethod("set" + property.getkey().substring(0, 1).touppercase() + property.getkey().substring(1), property.getvalue().getclass()); setter.invoke(pojo, property.getvalue()); } pojos.add(pojo); } } catch (exception e) { e.printstacktrace(); } (counter pojo : pojos) { log.e("result", "" + pojo.getid() + " " + pojo.gettype()); }
is there way parse specific array using,
gson
using duel parsing technique parse specific portion manually , rest of response in gson?
i want make dynamic objects wrapped pojo class.
you need 2 things.
- you need little bit of refactoring on model classes.
- you need add jsondeserializer handle dynamic parsing.
here's how it:
mainobject.java
..... @serializedname("point_of_sales") salespoints msales points .....
salespoints.java
private list<counter>mcounters; //getter & setters.
counter.java
serializedname("type") private string type; @serializedname("id") private int id; // use primitives instead of boxed primitives private string mcounterstring; // maybe need it.
now when run parsing you'll have this:
mainobject object = new gsonbuilder().registertypeadapter(salespoints.class, new jsondeserializer< salespoints >() { @override public salespoints deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { salespoints sales = new salespoints(); arraylist<counter>counters = new arraylist<counter>(); jsonarray salesjson = json.getasjsonarray(); for(jsonelement object : json.getasjsonarray()) { iterator<map.entry<string,jsonelement>> iterator = object.getasjsonobject().entryset().iterator(); while (iterator.hasnext())){ map.entry<string,jsonelement> entry = iterator.next(); string counterstring = entry.getkey(); jsonelement counterobject = entry.getvalue(); counter counter = context.deserialize(counterobject, counter.class); counter.setcounterstring(counterstring); counters.add(counter); } } sales.setcounters(counters); return sales; } }).create().fromjson(thejson);
now when mainobject
parsed contain salespoints
contains counters.
note: might misspelled words, or might forgot add getas..
methods because didn't have possibility test code, way go , can debug it.
edit
for retrofit you'll need this:
gson gson = new gsonbuilder().registertypeadapter(salespoints.class, new jsondeserializer< salespoints >() { @override public salespoints deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { salespoints sales = new salespoints(); arraylist<counter>counters = new arraylist<counter>(); jsonarray salesjson = json.getasjsonarray(); for(jsonelement object : json.getasjsonarray()) { iterator<map.entry<string,jsonelement>> iterator = object.getasjsonobject().entryset().iterator(); while (iterator.hasnext())){ map.entry<string,jsonelement> entry = iterator.next(); string counterstring = entry.getkey(); jsonelement counterobject = entry.getvalue(); counter counter = context.deserialize(counterobject, counter.class); counter.setcounterstring(counterstring); counters.add(counter); } } sales.setcounters(counters); return sales; } }).create(); new retrofit.builder().addconverterfactory(gsonconverterfactory.create(gson))//set remaining things fro retrofit builder.
Comments
Post a Comment