Jquery 无法分析AJAX响应中的嵌套JSON
我在jQuery/AJAX中编写了一段代码,用于查询Elasticsearch并根据搜索条件返回文档列表。我可以从Elasticsearch获取文档。我正在尝试解析文档并在网页中显示它们。以下是Elasticsearch索引的响应Jquery 无法分析AJAX响应中的嵌套JSON,jquery,json,Jquery,Json,我在jQuery/AJAX中编写了一段代码,用于查询Elasticsearch并根据搜索条件返回文档列表。我可以从Elasticsearch获取文档。我正在尝试解析文档并在网页中显示它们。以下是Elasticsearch索引的响应 { "took": 12, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.617034,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "uhp0sW0BO2MOKYEQa-CB",
"_score": 1.617034,
"_source": {
"MsgTime": "2019-10-09T16:57:39.829Z",
"OrganizationID": "nasa",
"UserID": "USER1",
"TravelID": "1234",
"MachineID": "9099",
"Stage": "ingested"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "sxpnsW0BO2MOKYEQ9eAo",
"_score": 1.617034,
"_source": {
"MsgTime": "2019-10-09T16:44:03.102Z",
"OrganizationID": "nasa",
"UserID": "USER1",
"TravelID": "201710283fc113afa731459285b55d94bb8ddf02",
"MachineID": "9099",
"Stage": "processed"
}
}
]
}
}
对于“hits”下的每个“\u source”元素,我试图在网页上实现的输出如下所示。如果适用,这些值将不同
"MsgTime": "2019-10-09T16:57:39.829Z",
"OrganizationID": "nasa",
"UserID": "USER1",
"TravelID": "1234",
"MachineID": "9099",
"Stage": "ingested"
我尝试使用以下代码在jQuery中将输出解析为AJAX响应(仅解析部分)
在解析部分中,我无法获得最终输出。我不确定我最后的$。每个循环是否正确
如果需要,我可以提供更多信息。我已经排除了这里的HTML
提前感谢,,
尼克我提取了你的解析语句。基本上可以简化为以下内容:
obj=
{“take”:12,“timed\u out”:false,“u shards”:{“total”:1,“successful”:1,“skiped”:0,“failed”:0},“hits”:{“total”:{“value”:4,“relationship”:“eq”},“max\u score”:1.617034,“hits”:[{“index”:“test”、“\u type”:“\u doc“美国宇航局”、“用户id”:“用户1”、“TravelID”:“1234”、“机器id”:“9099”、“阶段”:“摄入”}、{“测试”、“_类型”:“用户文档”、“_id”:“sxpnsW0BO2MOKYEQ9eAo”、“_分数”:1.617034”、“来源”:“MsgTime”:“2019-10-09T16:44:03.102Z”、“组织id”:“美国宇航局”、“用户id”:“用户1”、“TravelID”:“201710283FC113AFA74585B52”、“机器id”:“机器id”:9099,“阶段”:“已处理”}]}
console.log(obj.hits.hits.map(v=>v.\u source))
感谢您的回复。让我试试……感谢您的快速帮助。工作起来很有魅力。map()函数与map reduce相同吗?.map()
返回数组,而reduce((acc,curr)=>{…;return acc},acc\u initobj)
将在对象作为acc
通过所有回调函数调用发送后返回对象acc\u initobj
。
$(document).ready(function() {
$("#search").click(function() {
var textboxvalue = $("#trip_id").val();
$.ajax({
url: myURL,
type: "GET",
dataType: 'json',
data: query = {
q: "TravelID: " +textboxvalue,
pretty:'true',
size:100
},
success: function( result ) {
$.each( result, function( key, value ) {
$.each(value,function(hits,v){
$.each(v,function(i,hits){
$.each(hits,function(_source,v){
console.log(_source,v);
});
});
});
});