首先,您需要JSON的键。 所以我尝试使用fields而不只是elements
Iterator> attributeIterator = dataArray.path("attributes").fields();
while (attributeIterator.hasNext())
{
Map.Entry attribute = attributeIterator.next();
users.get(id).put(attribute.getKey(),
attribute.getValue().get(0).asText());
}
我不喜欢获取数组,所以我改成这个
Iterator> attributeIterator = dataArray.path("attributes").fields();
while (attributeIterator.hasNext())
{
Map.Entry attribute = attributeIterator.next();
users.get(id).put(attribute.getKey(),
attribute.getValue().elements().next().textValue());
}
之所以使用fields是因为我需要键值:
迭代器,可用于遍历对象节点的所有键/值对 ; 其他类型的空迭代器(无内容)
而且elements不包含键:
如果此节点是JSON Array或Object节点,则用于访问此Node的所有值节点的方法。 对于“对象”节点, 不包括字段名称(键),仅包括值。 对于其他类型的节点,返回空的迭代器。
这样可以填满地图。 我用jackson 2.9.4