2013-01-24 44 views
0

我有一个PHP脚本,返回这样的JSON编码结果:遍历weired JSON在Javascript

// 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

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

$.each(jsonp, function() { 
    $.each(this, function() { 
     console.log(this.txtshare); 
     console.log(this.dtzserver); 
     console.log(this.txtphonee164); 
     console.log(this.txtfinding); 
    }); 
}); 
0

在你的例子中,“val”指的是主“jsonp”数组中的嵌套数组。你需要一个嵌套$。每结构得到嵌套阵列状的内内容:

$.each(jsonp, function(index, value){ 
    $.each(value, function(index, innerval){ 
     console.log(innerval.txtshare); 
     //... and so on 
    }); 
}); 
+0

谢谢你这么mutch它的工作,你救了寻找修复:)感谢麻烦。 – user1818439