如何从json响应中提取对象

时间:2017-01-30 13:28:24

标签: javascript jquery json ajax elastic-stack

我目前正在JavaScript中进行帖子查询,返回一个resposne。我能够将此响应存储在变量中。

                xhr = new XMLHttpRequest();
                var url = "http://localhost:9200/test/_search?=pretty=true";
                xhr.open("POST", url, true);
                xhr.setRequestHeader("Content-type", "application/json");
                var data = JSON.stringify(myQuery);
                xhr.send(data);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == XMLHttpRequest.DONE) {
                        console.log(xhr.response)
                        }

                }

          }       

控制台输出

{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 54,
    "max_score" : 4.8836236,
    "hits" : [ {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAzviQ3keyPu6fdK",
      "_score" : 4.8836236,
      "_source" : {
        "message" : "o232870047\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:17.301Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o232870047",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAr_iQ3keyPu6fWd",
      "_score" : 4.8795,
      "_source" : {
        "message" : "o231380011\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:16.699Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o231380011",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAzviQ3keyPu6fdL",
      "_score" : 4.8795,
      "_source" : {
        "message" : "o232870052\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:17.302Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o232870052",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAvliQ3keyPu6fZJ",
      "_score" : 4.871201,
      "_source" : {
        "message" : "o232230020\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:16.845Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o232230020",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAzviQ3keyPu6fdN",
      "_score" : 4.871201,
      "_source" : {
        "message" : "o232870054\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:17.302Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o232870054",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAr_iQ3keyPu6fW2",
      "_score" : 4.7276993,
      "_source" : {
        "message" : "o231470025\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:16.721Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o231470025",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAvliQ3keyPu6fYe",
      "_score" : 4.7276993,
      "_source" : {
        "message" : "o232100007\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:16.810Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o232100007",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbBC8iQ3keyPu6for",
      "_score" : 4.7276993,
      "_source" : {
        "message" : "o234410029\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:18.334Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o234410029",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAr_iQ3keyPu6fXg",
      "_score" : 4.327994,
      "_source" : {
        "message" : "o231920008\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:16.751Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "host" : "ATGDEV11",
        "OrderNumber" : "o231920008",
        "ProductName" : "20033897\r"
      }
    }, {
      "_index" : "main12",
      "_type" : "logs",
      "_id" : "AVnfbAxdiQ3keyPu6fa3",
      "_score" : 4.327994,
      "_source" : {
        "message" : "o232520053\t20033897\r",
        "@version" : "1",
        "@timestamp" : "2017-01-27T10:15:17.124Z",
        "path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
        "OrderNumber" : "o232520053",
        "ProductName" : "20033897\r"
      }
    } ]
  },
  "aggregations" : {
    "movies_like_terminator" : {
      "doc_count" : 54,
      "buckets" : [ {
        "key" : "20033894",
        "doc_count" : 54,
        "score" : 43.870370370370374,
        "bg_count" : 54
      }, {
        "key" : "20019702",
        "doc_count" : 14,
        "score" : 8.788675506782502,
        "bg_count" : 18
      }, {
        "key" : "20178852",
        "doc_count" : 5,
        "score" : 3.3696273433927755,
        "bg_count" : 6
      }, {
        "key" : "20151147",
        "doc_count" : 4,
        "score" : 3.249657064471879,
        "bg_count" : 4
      }, {
        "key" : "20109408",
        "doc_count" : 7,
        "score" : 3.002347789384826,
        "bg_count" : 13
      }, {
        "key" : "6001009817444",
        "doc_count" : 5,
        "score" : 2.875024495394866,
        "bg_count" : 7
      }, {
        "key" : "20111529",
        "doc_count" : 3,
        "score" : 2.4372427983539096,
        "bg_count" : 3
      }, {
        "key" : "200100012",
        "doc_count" : 3,
        "score" : 2.4372427983539096,
        "bg_count" : 3
      }, {
        "key" : "2006667281",
        "doc_count" : 3,
        "score" : 2.4372427983539096,
        "bg_count" : 3
      }, {
        "key" : "20049703236",
        "doc_count" : 3,
        "score" : 2.4372427983539096,
        "bg_count" : 3
      } ]
    }
  }
}

如果向下滚动到此回复的底部,我想从

中提取“密钥”
"aggregations" : {
    "movies_like_terminator" : {
      "doc_count" : 54,
      "buckets" : [ {
        "key" : "20033894",
        "doc_count" : 54,
        "score" : 43.870370370370374,
        "bg_count" : 54
      }, {
        "key" : "20019702",
        "doc_count" : 14,
        "score" : 8.788675506782502,
        "bg_count" : 18

请告知我该如何做到这一点。

如何抓住这个物体?
我应该使用循环迭代对象吗?

0 个答案:

没有答案