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

Posted

技术标签:

【中文标题】如何解析具有动态键名的嵌套 json【英文标题】:how to parse a nested json with dynamic key names 【发布时间】:2017-07-13 15:44:30 【问题描述】:

我有以下格式的 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
                                        
                                    
                                ]
                            
                        
                    ]
                
            
        ]
    

【问题讨论】:

您使用的是什么 JSON 库?到目前为止的 Java 代码在哪里? @cricket_007,我正在使用 jackson,并且添加了有问题的 java 代码。 【参考方案1】:

好的,找到问题并找到解决方案。应该是递归的json解析,先获取所有的bucket,然后遍历得到各个key。

【讨论】:

以上是关于如何解析具有动态键名的嵌套 json的主要内容,如果未能解决你的问题,请参考以下文章

如何使用json方法解析android中没有键名的数组内的嵌套json编码数组?

如何反序列化具有动态(数字)键名的子对象?

在 Typescript 中访问具有动态键名的状态 [重复]

Joi 对象验证:如何验证具有未知键名的值?

更改没有键名的json值

连接两个具有不同键名的表