jQuery嵌套.each循环JSON成功

时间:2014-06-20 17:50:34

标签: javascript jquery ajax json

参考这篇文章 jQuery loop over JSON result from AJAX Success?

返回JSON(如Firebug中所示):

[
   {"NUMBER":"N02939667","BARCODE":"B000615994","LOAD_FK":"7813","CRO":"VONS"},
   {"NUMBER":"N02939667","BARCODE":"B000607696","LOAD_FK":"8875","CRO":"VONS"}
]

我的AJAX与嵌套的.each函数:

$.ajax({
        type: "POST",
        url: "get_shipment_by_nnumber.php",
        data: searchItemDataString,
        dataType: 'json',
        success: function(g){
            if ($.isEmptyObject(g[0].LOAD_FK)){
                //validate json returned
            }else{
                  $('.BGresults').empty();
                  $.each(g, function() {
                      $.each(this, function(k, v) {
                          var result = '<strong>     ' + searchItem + ':  (' + v.BARCODE + ') (Reg ID = ' + v.LOAD_FK + ')  (' + v.CRO + ')</strong><br>';
                          $('.BGresults').append(result);
                      });
                  });                   
             };
         }
    })

我的输出是在每个k / v对上迭代而没有找到值。我猜是因为我有两个物体,每个物体有5 k / v对,我得到10个输出。我在s.o上看了类似的帖子。但我只是没有看到对我有意义的答案。有人可以指出我的错误。谢谢。

N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)
N02939667.1-1: (undefined) (Reg ID = undefined) (undefined)

3 个答案:

答案 0 :(得分:2)

var data = [
   {"NUMBER":"N02939667","BARCODE":"B000615994","LOAD_FK":"7813","CRO":"VONS"},
   {"NUMBER":"N02939667","BARCODE":"B000607696","LOAD_FK":"8875","CRO":"VONS"}
];

$.each(data, function() { 
    var output = [];

    $.each(this, function(k, v) {
       output.push(k, v);
    });

    console.log(output.join(' '));

});

答案 1 :(得分:1)

要使当前代码正常工作,您需要在第二个嵌套循环中引用$(this)而不是this。另外,你在我的小提琴中注意到我硬编码searchItem的价值,因为你没有在你的例子中提供它。

fiddle with simulated data

$.ajax({
        type: "POST",
        url: "get_shipment_by_nnumber.php",
        data: searchItemDataString,
        dataType: 'json',
        success: function(g){
            if ($.isEmptyObject(g[0].LOAD_FK)){
                //validate json returned
            }else{
                  $('.BGresults').empty();
                  $.each(g, function() {
                      $.each($(this), function(k, v) {
                          var result = '<strong>     ' + searchItem + ':  (' + v.BARCODE + ') (Reg ID = ' + v.LOAD_FK + ')  (' + v.CRO + ')</strong><br>';
                          $('.BGresults').append(result);
                      });
                  });                   
             };
         }
    })

答案 2 :(得分:1)

考虑这个例子:

data = [
       {"NUMBER":"N02939667","BARCODE":"B000615994","LOAD_FK":"7813","CRO":"VONS"},
       {"NUMBER":"N02939667","BARCODE":"B000607696","LOAD_FK":"8875","CRO":"VONS"}
       ]

//for each value in data => this ("this" is a variable containing an element of data)
$.each(data, function(){   //in this function you can access "this", and element of data
    $.each(this, function(k,v){  //inner loop iterated over the key-value pairs of "this"
        console.log(k +"==="+v);
        })
});

此输出:

NUMBER===N02939667 
BARCODE===B000615994 
LOAD_FK===7813 
CRO===VONS 
NUMBER===N02939667 
BARCODE===B000607696 
LOAD_FK===8875 
CRO===VONS 

k获取JSON对象中每个键的值,v获取相应的值。您实际上想要做的是从JSON对象获取特定值并将其粘贴到某些HTML中。这样做:

$.each(data, function(){  
    var result = '<strong>     ' + searchItem + ':  (' + this.BARCODE + ') (Reg ID = ' + this.LOAD_FK + ')  (' + this.CRO + ')</strong><br>';
    $('.BGresults').append(result);
});

使用this.key访问JSON对象中该键的给定值。