我有以下JSON:
{
"items": [
{
"id": "1",
"name": "John",
"location": {
"town": {
"id": "10"
},
"address": "600 Fake Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "2",
"name": "Sarah",
"location": {
"town": {
"id": "10"
},
"address": "76 Evergreen Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "3",
"name": "Hamed",
"location": {
"town": {
"id": "20"
},
"address": "50 East A Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
}
]
}
我需要得到这样的东西,计算每个townId
出现的次数:
[ { "10": 2 }, {"20": 1 }]
我正在努力寻找最有效的方法来做到这一点。有什么想法吗?
答案 0 :(得分:1)
最有效的方法是在StringBuilder中加载String并删除所有换行符和空格。然后搜索"town":{"id":"
字符串索引(城镇起始索引),然后搜索结束索引(字符串“”}“)。使用这2个索引,您可以提取城镇ID并计算它们。
无需将JSON反序列化为POJO对象:)并从POJO中通过xpath提取值。