Javascript无法访问对象键值......它们周围有引号

时间:2014-03-05 23:51:16

标签: javascript string variables object key

所以我需要访问此对象中的键值“key”:

{
  "__v": 0,
  "_id": "5317b71c902ff0080046349f",
  "user": "530ef48599c41200009bad9f",
  "email": "asdfa@sdgdf.com",
  "btcId": "9HjBb9eUhyXMKuVxSrTSkg",
  "name": "Bitcoin Payments",
  "dlFile": "a43ca076-4802-4cca-9648-82b2569ffc60.docx",
  "key": "3518d5ce-badf-495c-a08f-dc28bb6d8a64",
  "created": "2014-03-05T23:45:32.480Z"
}

但此代码不起作用:

console.log(body.key) -> Undefined
console.log(body['key'] -> undefined
console.log(body['"key"'] -> undefined

body是存储对象的位置。

我认为它与键的字符串有关?或者在他们周围有“”? 但是我没有创建body变量,所以我无法控制它...

关于如何使其发挥作用的任何想法?

1 个答案:

答案 0 :(得分:6)

However I'm not creating the body variable so I can't control this...

这听起来像GET请求,所以如果是JSON数据,请用JSON.parse解析:

console.log(JSON.parse(body).key)