I read in a valid JSON file, which has the format shown below (I have no control in that) with only values for the root nodes, using:
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
JsonNode rootNode = jsonMapper.readTree(belowString);
- How do I get the root nodes names (First and second below), which I do not know?
- Subsequently, I also need to read the attending value
{
"First": [{
"name": "Bill",
"groupName": "team1",
"groupType": "golf",
"info": [{
"name": "George",
"groupName": "Caddy"
}],
"attending": false
},
{
"name": "Fred",
"groupName": "team2",
"groupType": "golf",
"info": [{
"name": "Todd",
"groupName": "caddy"
}],
"attending": false
},
{
"name": "Mike",
"groupName": "team3",
"groupType": "golf",
"info": [{
"name": "Peter",
"groupName": "caddy"
}],
"attending": false
}
],
"Second": [{
"name": "Alan",
"groupName": "team4",
"groupType": "golf",
"info": [{
"name": "Tony",
"groupName": "caddy"
}],
"attending": false
}]
}
The accepted answer solved #1. This is the resolution I used for #2 to access the nested nodes:
while (iter.hasNext()) {
Map.Entry<String, JsonNode> entry = iter.next();
System.out.println("key: " + entry.getKey());
System.out.println("value: " + entry.getValue());
if (entry.getValue().isArray()) {
JsonNode attending = entry.getValue().get(1).get("attending");
System.out.println("attending = " + attending.toString());
}
}