我想遍历一个json对象的所有节点,写出一个普通的key-value map,如下:
{
"name": [
{
"first": "John",
"last": "Doe",
"items": [
{
"name": "firstitem",
"stock": 12
},
{
"name": "2nditem",
"stock:" 23
}
]
}],
"company": "John Company"
}
应该导致:
name-first-1=John
name-last-1=Doe
name-items-name-1-1=firstitem (meaning the list index is always appended at the end of the name)
name-items-name-1-2=2nditem
company=John Company
这是获取 json 字符串作为 json 对象的方法:
ObjectMapper mapper = new ObjectMapper(); //using jackson
JsonNode root = mapper.readTree(json);
//TODO how loop all nodes and subnodes, and always get their key + value?
但是我现在如何遍历所有节点并提取它们的密钥和内容呢?
原文由 membersound 发布,翻译遵循 CC BY-SA 4.0 许可协议
这对你有用:
对于
json
你给出的输出将是: