0

私は次のように構築された1つのjsonオブジェクトを持っています:

<?
$t['w'][$id]['marchin']=$machin;
$t['w'][$id]['c'][$id_com]['machin']=$machin;    
echo json_encode($t);
?>

私はこのようにオブジェクトを閲覧します

// here i access to all the $t['w'][$id]

$.each(data.w, function(k,v){

              var that=this;
              doSomething();

              // now i want to access to all the $t['w'][$id]['c']
               $.each(that.c, function(k1,v1){
                     doSomething();       
               });

});

しかし、ここで2番目に各jqueryはエラーを起こします..すべての$ t ['w'] [$ id] ['c']にアクセスする方法?!

ありがとうございました


OK私は試しました:

              $.each(data.w, function(k,v){
                  var that = $.parseJSON(this);
                        doSomething();

                    $.each(that[k]['c'], function(k1,v1){
                        doSomething();

                 });

       });

しかし、それは再び機能しません、

これが私のjsonの例です。

{"w":
   {"3":
      {"test":"test","c":
        {"15":
           {"test2":"test2"}
        }
      }
    }
}
4

2 に答える 2

1

Data ...

var data = {"w":
   {"3": {
       "test":"test",
       "c": {
          "15": {"test2":"test2"}
       }
      }
    }
};

Loop ...

$.each(data.w, function(key, value){
    // you are now lopping over 2nd dimension
    // On each loop, 'key' will be equal to another [$id] value
    // since you know you'd like to crawl 'c' sub entry, you can reference it
    // and loop over it
    $('body').append('<h3>'+key+'</h3>');
    if( this['c'] )
    $.each(this['c'], function(k,v){
        // Here you have access to ['c'][...]
        $('body').append('<span>'+k+'</span>');
    });
}); 
于 2012-09-16T21:42:34.810 に答える
0

You can do this without .each at all:

var hsh = $.parseJSON(data.w);

for (var i in hsh) {
  var that = hsh[i];
  doSomething();

  for (var j in hsh[i].c) {
    doSomething();
  }
}
于 2012-09-16T21:43:06.813 に答える