Javascript 获取另一个json对象(非数组)中的json对象
我得到了这个结构:Javascript 获取另一个json对象(非数组)中的json对象,javascript,json,parent,names,Javascript,Json,Parent,Names,我得到了这个结构: { "parent": { "child-parent-1": { "att1": null, "att2": null, }, "child-parent-2": { "att1": null, "att2": null, } }} 我需要得到的是child-parent-1和child-parent-2的名字,而不知道它们的名
{
"parent": {
"child-parent-1": {
"att1": null,
"att2": null,
},
"child-parent-2": {
"att1": null,
"att2": null,
}
}}
我需要得到的是child-parent-1和child-parent-2的名字,而不知道它们的名字。。。因为它们是动态生成的,就像哈希代码askdl1km2lkaskjdnzkj2138一样
尝试了迭代,但无法使其工作。我总是得到子属性键/值对。或包含所有对象的整个父对象。需要得到我上面提到的父母的名字
我该怎么做
提前感谢。迭代对象应该可以:
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}
迭代对象应该可以工作:
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}
是的,这是一个愚蠢的问题,尝试了很多东西,但还没有尝试,哈哈;制造了东西:啊,这是一个愚蠢的问题,尝试了很多东西,但还没有尝试过lol.alertkey;做了件事:解决!谢谢你的快速回复!可能的重复问题已解决!谢谢你的快速回复!可能重复的