0

我有一个 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
4

2 回答 2

0

你有一个 3 维数组,所以你需要两个循环:

$.each(jsonp, function() {
    $.each(this, function () {
        console.log(this.txtshare);
        console.log(this.dtzserver);
        console.log(this.txtphonee164);
        console.log(this.txtfinding);
    });
});
于 2013-01-24T00:40:37.137 回答
0

在您的示例中,“val”指的是主“jsonp”数组中的嵌套数组。您需要一个嵌套的 $.each 结构来获取嵌套数组的内部内容,例如:

$.each(jsonp, function(index, value){
    $.each(value, function(index, innerval){
        console.log(innerval.txtshare);
        //... and so on
    });
});
于 2013-01-24T00:42:17.553 回答