java - Gson deserialize json of embedded member -


i have following sample json:

{  "birds":[   {      "id":"sampleid",      "istest":true,      "externalid":{         "main":[            "123abc"         ],         "sub":[            "456"         ]      },      "dinos":[       ],      "rhinos":[         {            "id":"super100id",            "istest":true,            "externalid":{               "famousid":[                  "23|45"               ]            },            "dinos":[             ],            "pelicans":[               {                  "id":"d4clik",                  "istest":true,                  "bird":null,                  "crazyarray":[                   ]               },               {                  "id":"did123",                  "type":"b",                  "name":"tonie",                  "istest":true,                  "bird":null,                  "subspecies":[                   ]               }            ]         }      ]   } ], "metadata":{   "count":1 } } 

i want use gson deserialize json string , value of "famousid" member only.

i have looked through other answers , seems absolutely need create classes this.

would possible deserialize without mapping pojos, using jsonparser, jsonelement, jsonarray, etc? have tried several permutations of no success.

i have tried following code not working expected:

jsonobject o = new jsonparser().parse(jsonresponsestring).getasjsonobject();  gson gson = new gson(); enterprises ent = new enterprises(); ent = gson.fromjson(o, enterprises.class);   @getter @setter class birds {     @jsonproperty("rhinos")     list<rhino> rhinos = new arraylist<rhino>(); }  @getter @setter class rhino {     @jsonproperty("externalid")     externalid externalid; }  @getter @setter @jsonpropertyorder({     "famousid"     }) class externalid {     @jsonproperty("famousid")     list<string> famousid = new arraylist<string>(); }  

unfortunately not work either, guess 2 part question...is possible deserialize , string value famousid want, , incorrect current class structure?

you've done. added root class(enterprises) json structure.

class enterprises {     list<birds> birds; }  class birds {     list<rhino> rhinos; }  class rhino {     externalid externalid; }  class externalid {     list<string> famousid; }  

run below code:

jsonobject o = new jsonparser().parse(jsonresponsestring).getasjsonobject(); gson gson = new gson();  enterprises enterprises = gson.fromjson(o, enterprises.class); system.out.println("famousid:" + enterprises.birds.get(0).rhinos.get(0).externalid.famousid.get(0)); 

output:

famousid:23|45 

or if don't want use pojo classes:

jsonobject o = new jsonparser().parse(jsonresponsestring).getasjsonobject(); jsonarray birdsjsonarray = (jsonarray) o.get("birds"); jsonarray rhinosjsonarray = (jsonarray)((jsonobject)(birdsjsonarray.get(0))).get("rhinos"); jsonobject externalidjsonobject = (jsonobject)((jsonobject)(rhinosjsonarray.get(0))).get("externalid"); jsonarray famousidjsonarray = (jsonarray)externalidjsonobject.get("famousid");  system.out.println("famousid:" + famousidjsonarray.get(0)); 

output:

famousid:23|45 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -