解析复杂的JSON文件“undefined”

时间:2018-02-09 10:42:40

标签: javascript json

在尝试解析此JSON文件时,我得到了“undefined”:

 {
"responses": [
    {
      "labelAnnotations": [
        {
          "mid": "/m/01yrx",
          "description": "cat",
          "score": 0.9926739,
          "topicality": 0.9926739
        },
        {
          "mid": "/m/01l7qd",
          "description": "whiskers",
          "score": 0.9639658,
          "topicality": 0.9639658
        },
        {
          "mid": "/m/083jv",
          "description": "white",
          "score": 0.9582038,
          "topicality": 0.9582038
        },
        {
          "mid": "/m/0k0pj",
          "description": "nose",
          "score": 0.9425352,
          "topicality": 0.9425352
        },
        {
          "mid": "/m/06z04",
          "description": "skin",
          "score": 0.92025506,
          "topicality": 0.92025506
        }
      ]
    }
  ]
}

此文件来自Google Vision API的XMLHttpRequest,这就是我正在打印的“说明”字段:

e.onload=function(){
  var i= JSON.parse(e.response);
  value = i.responses[0]["description"];
  alert(value);
};

1 个答案:

答案 0 :(得分:2)

“undefined”来自以下声明

value = i.responses[0]["description"];

因为labelAnnotations是数组中的一个对象,您将使用

进行描述

value = i.responses[0].labelAnnotations[0]["description"];