在尝试解析此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);
};
答案 0 :(得分:2)
“undefined”来自以下声明
value = i.responses[0]["description"];
因为labelAnnotations是数组中的一个对象,您将使用
进行描述 value = i.responses[0].labelAnnotations[0]["description"];