java - How to deserialize json string into object -
{ "locallocationid [id=1]":{ "type":"folderlocation", "id":{ "type":"locallocationid", "id":1 }, "parentid":{ "type":"locallocationid", "id":0 }, "name":"test", "accessibletouser":true, "defaultlocation":false, "timezoneid":"asia/calcutta", "children":[] }, "locallocationid [id=0]":{ "type":"folderlocation", "id":{ "type":"locallocationid", "id":0 }, "parentid":null, "name":"locations", "accessibletouser":false, "defaultlocation":false, "timezoneid":"asia/calcutta", "children":[{ "type":"locallocationid", "id":1 }] }, "allallowedchildren":[{ "type":"locallocationid", "id":1 }] }
how deserialize above string java object.
class im using is
public class tree { @serializedname("allallowedchildren") private list<id> allallowedchildren; @serializedname("locallocationid") private map<string, locallocationid> locallocationid; public class locallocationid { @serializedname("type") private string type; @serializedname("name") private string name; @serializedname("accessibletouser") private boolean accessibletouser; @serializedname("defaultlocation") private boolean defaultlocation; @serializedname("timezoneid") private string timezoneid; @serializedname("id") private id id; @serializedname("parentid") private id parentid; @serializedname("children") private list<id> children; public string gettype() { return type; } public string getname() { return name; } public boolean isaccessibletouser() { return accessibletouser; } public boolean isdefaultlocation() { return defaultlocation; } public string gettimezoneid() { return timezoneid; } public id getid() { return id; } public id getparentid() { return parentid; } public list<id> getchildren() { return children; } } public class id { private string type; private integer id; public string gettype() { return type; } public integer getid() { return id; } } public list<id> getallallowedchildren() { return allallowedchildren; } public map<string, locallocationid> getlocallocationid() { return locallocationid; } }
@kedar
i'll assume in control of how json input string created. think json string not formatted correctly default gson deserialization of map types.
i have modified input string consideration , results in non null locallocationid
{ "locallocationid":[ [ "1", { "type":"folderlocation", "id":{ "type":"locallocationid", "id":1 }, "parentid":{ "type":"locallocationid", "id":0 }, "name":"test", "accessibletouser":true, "defaultlocation":false, "timezoneid":"asia/calcutta", "children":[] } ], [ "2", { "type":"folderlocation", "id":{ "type":"locallocationid", "id":0 }, "parentid":null, "name":"locations", "accessibletouser":false, "defaultlocation":false, "timezoneid":"asia/calcutta", "children":[{ "type":"locallocationid", "id":1 }] } ] ], "allallowedchildren":[{ "type":"locallocationid", "id":1 }] }
please comment if assumption input string incorrect.
edit 1: since input cannot modified, consider writing custom deserializer. below way register custom deserialisation class
gsonbuilder gsonb = new gsonbuilder(); gsonb.registertypeadapter(tree.class, new treedeserializer()); gson gson = gsonb.create();
below treedeserializer
public class treedeserializer implements jsondeserializer<tree> { public tree deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { tree out = new tree(); if (json != null) { jsonobject obj = json.getasjsonobject(); set<map.entry<string,jsonelement>> entries = obj.entryset(); (map.entry<string, jsonelement> e: entries) { if (e.getkey().equals("allallowedchildren")) { type ft = list.class; system.out.println(context.deserialize(e.getvalue(), ft)); // todo add tree out object } else { // locallocationid system.out.println(e.getkey()); system.out.println(context.deserialize(e.getvalue(), tree.locallocationid.class)); // todo add tree out object } } } return out; } }
here console output sysouts.
locallocationid [id=1] org.test.stackoverflowanswers.tree$locallocationid@464bee09 locallocationid [id=0] org.test.stackoverflowanswers.tree$locallocationid@f6c48ac [{type=locallocationid, id=1.0}] org.test.stackoverflowanswers.tree@589838eb
i have left todos in deserialiser you'll need write custom code inject values deserialisation tree class created. hope helps. can't provide full implementation, think partial solution
Comments
Post a Comment