如何遍历Json对象?
var obj = { "field": {"row1" : {"col1":10,"col2":20,"col3":30},"row2" : {"col1":20,"col2":30,"col3":40}}}
$(obj).each(function(i,val) {
child_obj =val
while(children = child_obj.children() ) {
child_obj = children .children()
}
});当我在JSON对象上调用子函数时,它没有得到子函数。
子函数是否只适用于DOM元素,而不适用于JSON?
如何遍历JSON对象并获取列值?
发布于 2014-03-26 14:16:23
var obj = { "field": {"row1" : {"col1":10,"col2":20,"col3":30},"row2" : {"col1":20,"col2":30,"col3":40}}}
$.each(obj.field,function(i,val){
console.log(val);
$.each(val, function(col, colVal){
console.log(col);
console.log(colVal);
});
})看一下这个迭代。jsfiddle
发布于 2014-03-26 14:31:50
这是你需要的吗?请查收
var obj = { "field": {"row1" : {"col1":10,"col2":20,"col3":30},"row2" : {"col1":20,"col2":30,"col3":40}}}
$.each(eval(obj.field), function(i, val){
console.log(val.col1+','+val.col2+','+val.col3);
});发布于 2014-03-27 03:01:40
遍历JSON对象
Demo
var root = {
leftChild: {
leftChild: {
leftChild: null,
rightChild: null,
data: 42
},
rightChild: {
leftChild: null,
rightChild: null,
data: 5
}
},
rightChild: {
leftChild: {
leftChild: null,
rightChild: null,
data: 6
},
rightChild: {
leftChild: null,
rightChild: null,
data: 7
}
}
};
function getLeaf(node) {
while(node instanceof Object) {
if (node.leftChild) {
node = getLeaf(node.leftChild);
} else if (node.rightChild) {
node = getLeaf(node.rightChild);
} else { // node must be a leaf node
return node;
}
console.log(node);
}
}
alert(getLeaf(root).data);https://stackoverflow.com/questions/22652560
复制相似问题