如何访问此javascript对象的属性?

时间:2012-12-02 18:13:11

标签: javascript json

  

可能重复:
  I have a nested data structure / JSON, how can I access a specific value?

var response = 
{
    "members": [{
        "hello": "goodbye",
        "foo": "bar"
    }],
    "tags": {
        "1065677": [{
            "tag_id": "244223",
            "tag_name": "lol-wat",
            "user_id": "1065677"
        }],
        "1065678": [{
            "tag_id": "244224",
            "tag_name": "baby",
            "user_id": "1065678"
        }]
    }
}

这可以转储整个对象:

console.log( response.tags[1065678] );

我现在想要获得一个键的特定值,即baby

console.log( response.tags[1065678].tag_name );
console.log( response.tags[1065678]['tag_name'] );

以上都没有,为什么?

1 个答案:

答案 0 :(得分:3)

“1065678”不是一个对象,但它是一个数组,所以试试

response.tags[1065678][0]['tag_name']

如果您想访问下面显示的对象,可以这样写:

"1065678": {
   "tag_id": "244224",
   "tag_name": "baby",
   "user_id": "1065678"
}