[英]how to parse a nested json with dynamic key names
我有以下格式的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,但是却不知道如何获取动态键并形成键/值对的Map。
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
}
}
]
}
}
]
}
}
]
}
}
}
好,找到问题并找到解决方案。 应该是递归的json解析,以首先获取所有存储桶,然后对其进行迭代以获取单个密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.