我有以下格式的JSON结果,JSON Lint将其显示为"有效响应"。
我的问题是:如何访问"聚合"的内容?并从嵌套的子对象中提取值,其中键是动态的。我的反应对象如下:
[
{
"metric_date":"2017-06-01 12:06:00",
"date_type":"Month",
"datacenter":"AWS-US-West-Portland",
"avg_avail_secs":1036800
},
{
"metric_date":"2017-06-01 12:06:00",
"date_type":"Month",
"datacenter":"Colo-US-East-Ashburn",
"avg_avail_secs":1036800
},
.....
]
我尝试了几种组合来使用Jackson递归解析json,但没有想到如何获取动态键并形成键/值对的映射。
private Object parseValue(Object inputObject, List<String> keys) throws JSONException {
Object outputObject = null;
if (null != inputObject) {
if (inputObject instanceof JSONArray) {
outputObject = parseJSONArray( (JSONArray) inputObject, keys);
} else if (inputObject instanceof JSONObject) {
outputObject = parseJSONObject( (JSONObject) inputObject, keys);
} else if (inputObject instanceof String || inputObject instanceof Boolean || inputObject instanceof Integer) {
outputObject = inputObject;
}
}
return outputObject;
}
JSON
{
"took": 201,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 63612,
"max_score": 0,
"hits": []
},
"aggregations": {
"metric_date": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1496318760000,
"key_as_string": "2017-06-01 12:06:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 1036800
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 1036800
}
}
]
}
}
]
}
},
{
"key": 1493640300000,
"key_as_string": "2017-05-01 12:05:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1491048240000,
"key_as_string": "2017-04-01 12:04:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2592000
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2592000
}
}
]
}
}
]
}
},
{
"key": 1488369780000,
"key_as_string": "2017-03-01 12:03:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1485950520000,
"key_as_string": "2017-02-01 12:02:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2419200
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2419200
}
}
]
}
}
]
}
},
{
"key": 1483272060000,
"key_as_string": "2017-01-01 12:01:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1480594320000,
"key_as_string": "2016-12-01 12:12:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1478002260000,
"key_as_string": "2016-11-01 12:11:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2592000
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2592000
}
}
]
}
}
]
}
},
{
"key": 1475323800000,
"key_as_string": "2016-10-01 12:10:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1472731740000,
"key_as_string": "2016-09-01 12:09:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2592000
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2592000
}
}
]
}
}
]
}
},
{
"key": 1470053280000,
"key_as_string": "2016-08-01 12:08:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1467374820000,
"key_as_string": "2016-07-01 12:07:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
},
{
"key": 1464782760000,
"key_as_string": "2016-06-01 12:06:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2592000
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2592000
}
}
]
}
}
]
}
},
{
"key": 1462104300000,
"key_as_string": "2016-05-01 12:05:00",
"doc_count": 3534,
"date_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Month",
"doc_count": 3534,
"datacenter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "AWS-US-West-Portland",
"doc_count": 1830,
"avg_avail_secs": {
"value": 2678400
}
},
{
"key": "Colo-US-East-Ashburn",
"doc_count": 1372,
"avg_avail_secs": {
"value": 2678400
}
}
]
}
}
]
}
}
]
}
}
}
答案 0 :(得分:0)
好的,得到了问题并找到了解决方案。它应该是一个递归的json解析,首先获取所有桶,然后迭代它以获得单独的密钥。