我得到了这个结构:
{
"parent": {
"child-parent-1": {
"att1": null,
"att2": null,
},
"child-parent-2": {
"att1": null,
"att2": null,
}
}}
我需要得到的是“child-parent-1”和“child-parent-2”的名字,但不知道它们的名字...因为它们是动态生成的,就像哈希码 (askdl1km2lkaskjdnzkj2138)。
尝试迭代,但我无法让它工作。我总是得到子属性(键/值对)。或者包含所有对象的整个父对象。需要获取我上面提到的 parent 姓名。
我该怎么做?
提前致谢。
最佳答案
迭代对象应该有效:
var parents = {
"parent": {
"child-parent-1": {
"att1": null,
"att2": null,
},
"child-parent-2": {
"att1": null,
"att2": null,
}
}
}
for(var key in parents.parent){ // parents.parent because the children are in a object in `parents`
console.log(key); // child-parent-1 / child-parent-2
console.log(parents.parent[key]); // The objects themselves.
}
对我来说,这个日志:
// child-parent-1
// Object {att1: null, att2: null}
// child-parent-2
// Object {att1: null, att2: null}
关于javascript - 在另一个 json 对象(不是数组)中获取 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14586501/