简体   繁体   English

在另一个json对象(不是数组)中获取json对象

[英]get json objects inside another json object (not array)

i got this structure: 我有这个结构:

{
"parent": {
      "child-parent-1": {
            "att1": null,
            "att2": null,
      },
      "child-parent-2": {
            "att1": null,
            "att2": null,
      }
 }}

What i need to get, is the "child-parent-1" and "child-parent-2" names without knowing their names... since they are dynamically generated, like a hash code (askdl1km2lkaskjdnzkj2138). 我需要得到的是不知道其名称的“ child-parent-1”和“ child-parent-2”名称,因为它们是动态生成的,就像哈希码(askdl1km2lkaskjdnzkj2138)一样。

Tried iterating but i couldn't make it work. 尝试迭代,但我无法使其工作。 I always get the child attributes (key/value pair). 我总是得到子属性(键/值对)。 Or the entire parent object with all the objects in it. 或包含所有对象的整个父对象。 Need to get the parent names i've mentioned above. 需要获取我上面提到的父母姓名。

How can I do this? 我怎样才能做到这一点?

Thanks in advance. 提前致谢。

Iterating the object should work: 迭代对象应该工作:

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.
}

For me, this logs: 对我来说,这记录:

// child-parent-1
// Object {att1: null, att2: null}
// child-parent-2
// Object {att1: null, att2: null}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM