【发布时间】:2018-07-16 11:35:03
【问题描述】:
我想遍历一个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?
但是我现在如何遍历所有节点并提取它们的密钥和内容?
【问题讨论】: