I have a JSON string that resembles the following:
{
"foo" : "bar",
"id" : 1,
"children":[
{
"some" : "string",
"id" : 2,
children : []
},
{
"some" : "string",
"id" : 2,
children : []
}
]
}
I do a JSON parse of this string, and that turns all objects into HashMaps and all arrays into HashMap[]s. My problem is I need a single recursive function to iterate through all nodes of this JSON structure in Java. How can I do this? I was thinking something like:
public HashMap findNode(boolean isArray, HashMap map, HashMap[] array){
//array stuff
if(isArray){
for(int i=0; i<array.length(); i++){
Object value = array[i];
if(value instanceof String)
System.out.println("value = "+value);
else if(value instanceof HashMap)
findNode(false, value, null);
else if(value instanceof HashMap[])
findNode(true, null, value);
}
//hashmap stuff
}else{
for(HashMap.Entry<String, Object> entry : map.entrySet()){
Object value = entry.getValue();
if(value instanceof String)
System.out.println("value = "+value);
else if(value instanceof HashMap)
findNode(false, value, null);
else if(value instanceof HashMap[])
findNode(true, null, value);
}
}
}