{
\"LocalLocationId [id=1]\":{
\"type\":\"folderlocation\",
\"id\":{
\"type\":\"locallocationid\",
\"id\":1
},
\"parentId\":
User JSONParser which is faster one.
Below is sample. there can be a btter example if you google. Hope this helps.
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=parser.parse(s);
System.out.println(obj);
s="[5,]";
obj=parser.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=parser.parse(s);
System.out.println(obj);
You can use Gson ..
String json = "Your json string "
Tree treeObj= new Gson().fromJson(json, Tree .class);
You can use ObjectMapper
from Jackson -
Tree deserializedTree = new ObjectMapper().readValue(jsonStringOfTree, Tree.class);
@Kedar
I'll assume you are in control of how the JSON input string is created. I think the JSON string is not formatted correctly for default GSON deserialization of Map types.
I have modified the input string for your consideration and this results in a 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 my assumption about the input string is incorrect.
EDIT 1: Since input cannot be modified, consider writing custom Deserializer. Below is the way to register custom deserialisation class
GsonBuilder gsonb = new GsonBuilder();
gsonb.registerTypeAdapter(Tree.class, new TreeDeserializer());
Gson gson = gsonb.create();
Below is the 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();
for (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 this back into the Tree out object
} else {
// LocalLocationId
System.out.println(e.getKey());
System.out.println(context.deserialize(e.getValue(), Tree.LocalLocationId.class));
// TODO add this back into the Tree out object
}
}
}
return out;
}
}
Here is the console output from the 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 the deserialiser where you'll need to write custom code to inject the values from deserialisation into the Tree class just created. Hope this helps. Can't provide full implementation, but I think this would be a partial solution