如何使用jQuery和getJSON访问这些json元素?

时间:2014-05-27 16:47:46

标签: jquery elasticsearch

我有一个弹性搜索查询返回的数组如下:

[
   [
      "took",
      2
   ],
   [
      "timed_out",
      false
   ],
   [
      "_shards",
      {
         "total":5,
         "successful":5,
         "failed":0
      }
   ],
   [
      "hits",
      {
         "total":1,
         "max_score":0.2712221,
         "hits":[
            {
               "_index":"myindex",
               "_type":"mytype",
               "_id":"1",
               "_score":0.2712221,
               "_source":{
                  "id":1465,
                  "level":"Mid-Level",
                  "first":"Kung-Wei",
                  "last":"Liu",
                  "designation":"Associate",
                  "company":"Sullivan & Cromwell",
                  "email":"89liu1017@gmail.com",
                  "phone":"phone",
                  "city":"Hong Kong",
                  "country":"China",
                  "industry":"Legal",
                  "status":"none",
                  "owner":"hector@fastmail.se",
                  "remarks":"looks unusal",
                  "experience":"unknown ",
                  "created_at":"2014-05-09 06:01:50.401076"
               }
            }
         ]
      }
   ]
]

使用getJSON调用,如何访问"指定"或" city"例如?我的jQuery代码是:

$(document).ready(function(){
$.getJSON("/search", function(data) {
    //var txt1 = 'You have ' +data["total_count"]+ ' campaigns';
    //$("#notification").html(txt1);
    var txt  = '<table><tr>';
     $.each( data, function( key, value ) {
        txt += '<tr><td><a href="#" class="a"  id="' +this["id"]+ '">' +this["name"]+ '</a></td>';
        txt += '<td>' +this["id"]+ '</td>';
        txt += '<td>' +this["first"]+ '</td>';
        txt += '<td>' +this["last"]+ '</td>';
        txt += '<td>' +this["designation"]+ '</td>';
        txt += '<td>' +this["company"]+ '</td>';
        txt += '<td>' +this["experience"]+ '</td>';
        });//end each
        txt += '</tr></table>';
 $("#results").append(txt);
    });//end json
});

this["variable"]不起作用!

感谢所有的帮助,谢谢你们。

2 个答案:

答案 0 :(得分:0)

你是说这个意思吗? JSFiddle

    var xxx = [
   [
      "took",
      2
   ],
   [
      "timed_out",
      false
   ],
   [
      "_shards",
      {
         "total":5,
         "successful":5,
         "failed":0
      }
   ],
   [
      "hits",
      {
         "total":1,
         "max_score":0.2712221,
         "hits":[
            {
               "_index":"myindex",
               "_type":"mytype",
               "_id":"1",
               "_score":0.2712221,
               "_source":{
                  "id":1465,
                  "level":"Mid-Level",
                  "first":"Kung-Wei",
                  "last":"Liu",
                  "designation":"Associate",
                  "company":"Sullivan & Cromwell",
                  "email":"89liu1017@gmail.com",
                  "phone":"phone",
                  "city":"Hong Kong",
                  "country":"China",
                  "industry":"Legal",
                  "status":"none",
                  "owner":"hector@fastmail.se",
                  "remarks":"looks unusal",
                  "experience":"unknown ",
                  "created_at":"2014-05-09 06:01:50.401076"
               }
            }
         ]
      }
   ]
];

    console.log(xxx[3][1].hits[0]._source.designation);
    console.log(xxx[3][1].hits[0]._source.city);

<强>更新

$(document).ready(function(){
$.getJSON("/search", function(data) {
var item = data[3][1].hits[0]._source;
    var txt  = '<table><tr>';
        txt += '<tr><td><a href="#" class="a"  id="' +item["id"]+ '">' +item["name"]+ '</a></td>';
        txt += '<td>' +item["id"]+ '</td>';
        txt += '<td>' +item["first"]+ '</td>';
        txt += '<td>' +item["last"]+ '</td>';
        txt += '<td>' +item["designation"]+ '</td>';
        txt += '<td>' +item["company"]+ '</td>';
        txt += '<td>' +item["experience"]+ '</td>';
        txt += '</tr></table>';
 $("#results").append(txt);
    });//end json
});

答案 1 :(得分:0)

你应该试试这个:

 var txt  = '<table><tr>';
 $.each( data, function( key, value ) {
    if(key == 3)
    {
     txt += '<td>' + value[1].hits[0]._source.designation + '</td>';   
     txt += '<td>' + value[1].hits[0]._source.city + '</td>'; 
    }
});
 txt += '</tr></table>';
 $("#results").append(txt);

示例小提琴:http://jsfiddle.net/robertrozas/7r275/