错误**未捕获的TypeError:从json提取数据时无法读取未定义的属性'名词'**

时间:2019-03-17 15:18:02

标签: javascript html json

我正在尝试从url获取数据,但是我遇到了错误

  

未捕获的TypeError:无法读取未定义**的属性“名词”,

谁能告诉我为什么出现此错误?

https://codepen.io/Thakur92411/pen/bZMObY?editors=1011

var googlelink = "https://googledictionaryapi.eu-gb.mybluemix.net/?define=apple&lang=en";

$.getJSON(googlelink, linkdata);

function linkdata(worddata) {
  console.log(worddata.meaning.noun.defination[0])
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

2 个答案:

答案 0 :(得分:0)

values = response.xpath("//td[./preceding-sibling::td[normalize-space()='EPS (Basic) - Growth']]/text()").extract() item['eps2014altrate'] = value[0] if 0 < len(values) else None item['eps2015altrate'] = value[1] if 1 < len(values) else None item['eps2016altrate'] = value[2] if 2 < len(values) else None item['eps2017altrate'] = value[3] if 3 < len(values) else None item['eps2018altrate'] = value[4] if 4 < len(values) else None worddata不是对象。它们都是数组。使用

noun

这是一个片段:

worddata[0].meaning.noun[0].definition
let googlelink = "https://googledictionaryapi.eu-gb.mybluemix.net/?define=apple&lang=en";

function linkdata(worddata) {
  console.log(worddata[0].meaning.noun)
  console.log(worddata[0].meaning.noun[0].definition)
}

$.getJSON(googlelink, linkdata);

答案 1 :(得分:-2)

一切看起来都不错,唯一的问题是您没有正确阅读响应。看到下面的代码供您参考。

apiLink = "https://googledictionaryapi.eu-gb.mybluemix.net/?define=apple&lang=en";
$.ajax({
  "url": apiLink,
    success: function(response){
    console.log(response[0].meaning.noun[0].definition);
  }
});