坚持使用JSON对象

时间:2012-05-24 20:15:30

标签: javascript jquery foreach

我有一个类似于

的JSON对象
{
   "mydata":[
            "one",
            "two",
            "three"
            ],
   "outside":[
               {
                 "vals":["four", "five", "six"],
                 "soso":{"seven", "eight", "nine"]               
               },
               {
                 "vals":["four", "five", "six"],
                 "soso":{"seven", "eight", "nine"]               
               },
               {
                 "vals":["four", "five", "six"],
                 "soso":{"seven", "eight", "nine"]               
               },
              ]
   "inside":[]

我试图用jquery运行$ .each on“outside”所以我可以从“外部”得到每个vals值的设置而且我没有得到任何东西但是我失败了我已经放弃了希望有人可以帮助

2 个答案:

答案 0 :(得分:2)

outside对象soso内应该如下所示

"soso":["seven", "eight", "nine"]然后

$.each(json.outside, function() {
    this.vals.each(function(index, val) {
       console.log(val); // output: "four", "five", "six"
    });

   this.soso.each(function(index, val) {
       console.log(val); // output: "seven", "eight", "nine
    });
});

答案 1 :(得分:1)

使用jQuery:

$.each(jsonObj.outside, function () {
    var i = this; // The current item
});