无法读取未定义的属性图片

时间:2017-02-20 10:13:09

标签: javascript json vuejs2

我有以下JSON

{
  "error": false,
  "data": {
    "id": 1,
    "name": "Jagadesha NH",
    "email": "example@gmail.com",
    "phone": "9986XXXXXX",
    "dob": "1991-06-01",
    "gender": "m",
    "active": 1,
    "created_at": "2017-02-19 21:33:04",
    "updated_at": "2017-02-19 21:33:04",
    "profile": {
      "id": 1,
      "user_id": 1,
      "picture": "https:\/\/placehold.it\/150x150",
      "about": null,
      "occupation": null,
      "created_at": "2017-02-19 21:33:04",
      "updated_at": "2017-02-19 21:33:04"
    }
  },
  "msg": ""
}

每当我尝试读取图片属性时,我都会收到错误

  

无法读取未定义的属性图片

我正在阅读

data.profile.picture

2 个答案:

答案 0 :(得分:0)

我发现它是这样的:

var x={
  "error": false,
  "data": {
    "id": 1,
    "name": "Jagadesha NH",
    "email": "example@gmail.com",
    "phone": "9986xxxxxx",
    "dob": "1904-06-01",
    "gender": "m",
    "active": 1,
    "created_at": "2017-02-19 21:33:04",
    "updated_at": "2017-02-19 21:33:04",
    "profile": {
      "id": 1,
      "user_id": 1,
      "picture": "https:\/\/placehold.it\/150x150",
      "about": null,
      "occupation": null,
      "created_at": "2017-02-19 21:33:04",
      "updated_at": "2017-02-19 21:33:04"
    }
  },
  "msg": ""
}
;

alert(x.data.profile.picture);

如果将json存储在变量中,则使用变量访问它,然后使用点(<<variable_name>>.data.profile.picture

答案 1 :(得分:0)

您收到错误,因为数据本身就是对象的关键或属性

var a = //Your json

    a.data.profile.picture