Java 如何解析具有动态键名的嵌套json

Java 如何解析具有动态键名的嵌套json,java,json,Java,Json,我有一个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

我有一个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;
}

}

好的,找到了问题并找到了解决方案。首先获取所有bucket,然后对其进行迭代以获取单个键,这应该是一个递归的json解析。

您使用的是什么json库?到目前为止Java代码在哪里?@cricket_007,我正在使用jackson,我已经添加了有问题的Java代码。
{
"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
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
}