Specificity of Jackson vs Json OR "Why is this a good reply?" -


i'm pretty new jackson & json have worked already, using object mappers, defining own classes , working object oriented. recently, decided teach myself android programming , - in the process - query data 3rd party webservice uses json encoding replies. 1 of replies confounds me.

i query number of objects, , this:

json:  [   {"num":"2","total":"2"},   {"id":"1234", ...more fields...},   {"id":"1235", ...more fields...} ] 

this doesn't parse standard jackson approches, since, way read it, indicates start of array first element being of different type of following elements. unable model pojo allow standard jackson mapping. (and speaking, didn't think correct syntax) can still work around basic string editing, figure must have misunderstood something. pardon me if silly question. appreciate , i'm looking forward responses.

edit 1: hot licks, confirmed self doubts =). can suggest proper way model in jackson, or case of jackson being more specific json?

there 2 parts this:

  1. is legal json
  2. is sensible data representation

for first question answer "yes"; second 1 subjective. claim "no", because there no real typed structure in java (or javascript matter), aside generic "list of maps"; not object model, odd data model json.

so while can bind using jackson like:

list<map<string,object>> list = (list<map<string,object>>) mapper.readvalue(jsonsource, list.class); 

it bit cumbersome use.

however. can use two-step processing: first above-mentioned "untyped" list; , convert maps actual objects want, like:

iterator<map<string,object>> = list.iterator(); headerob header = mapper.convertvalue(it.next(), headerob.class); // has 'num' , 'total' while (it.hasnext()) {   valueob value = mapper.convertvalue(it.next(), valueob.class); // has 'id', others } 

the reason there no single-phase method that, well, json structure described above has no natural object mapping. whoever designed not thinking through implications of choosing such structure, or perhaps wasn't adept @ oo.

for worth, more common model used stuff json sample contains like:

{   "num" : 2,   "total" : 2,   "entries" : [      { "id": ... },      ...   ] } 

which map java objects well.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -