javascript - 无法从 Google Place Search API 返回的 JSON 中提取数据

标签 javascript json reactjs google-maps-api-3

我正在从 Google Place Search API 获取一些数据,但我不知道为什么无法从中访问数据。下面是我的代码。

getData = (keyword, location, country) => {
    let dataURI = `${URI}${keyword}+${location}+${country}${API}`;
    var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
    targetUrl = dataURI
    fetch(proxyUrl + targetUrl)
    .then((res) => res.json())
    .then((data) => {
      console.log(data); //reurns JSON data
      console.log(data.results); //Returns Undefined
    })
    .catch((e)=> console.log(`Error! ${e.message}`));
}

当我写 console.log(data) 时,这将返回以下内容 JSON data

但是当我执行 console.log(data.results) 时,会返回 Undefined。

我也尝试过 JSON.parse(data) 但这给了我一个错误

Unexpected token o in JSON at position 1

最佳答案

你明白了

Unexpected token o in JSON at position 1

因为你的数据已经是一个对象了。无需解析它。 javascript解释器已经帮你解析了。 所以你应该尝试这样做 JSON.parse(JSON.stringify(data));

关于javascript - 无法从 Google Place Search API 返回的 JSON 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49938758/

相关文章:

json - 将json对象数据获取到Angular中的html前端

json - 使用 Go 解析复杂的 JSON

reactjs - Flow 会取代 PropTypes 吗?

javascript - 从 therubyracer(或 execjs)测试 ajax?

javascript - AngularJS 和正确使用 ngCloak

javascript - 满足条件后如何重置时间

json - Node 流 : remote stream is returning JSON, 但在 ('data' 上)显示缓冲区

javascript - 如何检查表格中的Checkbox是否被选中

reactjs - React 无状态函数组件 defaultProps 函数

javascript - 如何链接 addEventListener?