开发者

get JSON object attribute name

开发者 https://www.devze.com 2022-12-28 20:41 出处:网络
I know that I can retrieve \"session\" by using item.fields.name but what if I don\'t know in advance that the attribute is called \"name\". How can I retrieve the list of the attributes names in fiel

I know that I can retrieve "session" by using item.fields.name but what if I don't know in advance that the attribute is called "name". How can I retrieve the list of the attributes names in fields first.

[
    {
        "pk": 2,
        "model": "auth.group",
        "fields": {
开发者_JAVA技巧            "name": "session"
        }
    }
]


You can convert it to a javascript object, then iterate over the properties:

  eval("var object = " + that_json_thing);
  for (property in object) {
    alert(property);
  }

There is probably a "more correct" way of doing it, but this demonstrates the basic idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消