我有一个PHP脚本,它返回一个json编码的结果,如下所示:
// I added "var jsonp =" manually to make it an array variable.
var jsonp = [
[
{
"txtHide": "Y",
"data": "2012-12-21",
"phone": "+15879891300",
"Text": "Hello"
},
{
"txtHide": "Y",
"data": "2013-01-08",
"phone": "+15879891400",
"Text": "Issue"
},
{
"txtHide": "Y",
"data": "2013-01-19",
"phone": "+15879891040",
"Text": "This is a test "
}
],
[],
[
{
"txtHide": "Y",
"data": "2013-01-16",
"phone": "+142610588790",
"Text": "kkk 1"
},
{
"txtHide": "Y",
"data": "2013-01-18",
"phone": "+123610588790",
"Text": "Test 23"
},
{
"txtHide": "N",
"data": "2013-01-22",
"phone": "+123610588790",
"Text": "Hi"
}
],
[],
[],
[],
[
{
"txtHide": "Y",
"data": "2013-01-05",
"phone": "+221522988655",
"Text": "oo 12"
}
]
]
如何使用JQuery循环此JSON结果?
这是我做的,但它只打印了第四行:
$.each(jsonp, function(i,val) {
console.log(val[i].txtshare);
console.log(val[i].dtzserver);
console.log(val[i].txtphonee164);
console.log(val[i].txtfinding);
});
我的Firebug控制台输出:
Y
2012-12-21
+15879891300
Hello
答案 0 :(得分:0)
你有一个3维数组,所以你需要两个循环:
$.each(jsonp, function() {
$.each(this, function () {
console.log(this.txtshare);
console.log(this.dtzserver);
console.log(this.txtphonee164);
console.log(this.txtfinding);
});
});
答案 1 :(得分:0)
在您的示例中,“val”指的是主“jsonp”数组中的嵌套数组。你需要一个嵌套的$ .each结构来获取嵌套数组的内部内容,如:
$.each(jsonp, function(index, value){
$.each(value, function(index, innerval){
console.log(innerval.txtshare);
//... and so on
});
});