I'm trying to parse a JSON ArrayNode in Java but I'm having some issues.
The object is as follows:
{
"type": "type",
"id": "id",
"attributes": {
"x": [ "x.value" ],
"y": [ "y.value" ],
"z": [ "z.value" ]
}
}
I'm parsing it as follows:
Map<String, Map<String, String>> users = new HashMap<>();
Iterator<JsonNode> arrayIterator = dataArray.elements();
while (arrayIterator.hasNext())
{
JsonNode r = arrayIterator.next();
String id = r.get("id").asText();
users.put(id, new HashMap<>());
Iterator<JsonNode> attributeIterator = r.path("attributes").elements();
while (attributeIterator.hasNext())
{
JsonNode attribute = attributeIterator.next();
users.get(id).put(attribute.asText(),
attribute.elements().next().asText());
}
}
But I'm getting a map like this:
"" => z.value
I found out in Java' documentation that the attribute .asText() will return empty if it is not a value node. How can I get that name so my map is instead:
x => x.value
y => y.value
z => z.value