我有一个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();
});
});
但是在第二个每个jquery发生错误.. 如何访问所有$ t ['w'] [$ id] ['c']?!
谢谢
好的,我试过了:
$.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"}
}
}
}
}
答案 0 :(得分:1)
数据......
var data = {"w":
{"3": {
"test":"test",
"c": {
"15": {"test2":"test2"}
}
}
}
};
循环......
$.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>');
});
});
答案 1 :(得分:0)
你可以在没有.each的情况下做到这一点:
var hsh = $.parseJSON(data.w);
for (var i in hsh) {
var that = hsh[i];
doSomething();
for (var j in hsh[i].c) {
doSomething();
}
}