2017-07-13 4 views
0

JSONの結果に「有効な応答」と表示されるJSONの結果があります。動的なキー名でネストされたjsonを解析する方法

私の質問は、「集約」のコンテンツにアクセスし、キーが動的な入れ子の子オブジェクトから値を抽出する方法です。私は再帰的にどのように動的なキーを取得する考えを取得し、キー/値ペアの地図を形成ジャクソンを使用してではなく、JSONを解析するために、いくつかの組み合わせを試してみました

[ 
{ 
"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 
}, 
..... 
] 

:以下のように私の応答オブジェクトは、期待しました。

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

使用しているJSONライブラリを教えてください。これまでJavaコードはどこにありますか? –

+0

@ cricket_007、私はjacksonを使用しており、問題のJavaコードを追加しました。 – SSD

答えて

0

[OK]を、問題を持って、解決策を見つけました。すべてのバケットを最初に取得し、それを反復して個々のキーを取得するには、再帰的なjson解析が必要です。