参考这篇文章 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)
答案 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
的价值,因为你没有在你的例子中提供它。
$.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对象中该键的给定值。