【问题标题】:Can't Extract data from JSON returned by Google Place Search API无法从 Google Place Search API 返回的 JSON 中提取数据
【发布时间】:2018-04-20 09:45:34
【问题描述】:

我正在从 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) 时,这将返回未定义。

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

JSON 中位置 1 的意外标记 o

【问题讨论】:

  • 你需要解析类似let json = JSON.parse(data); console.log(json.results);的数据
  • 需要像这样解析响应。 JSON.parse(data) 之前访问 data.results
  • 这给了我一个错误Unexpected token o in JSON at position 1

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


【解决方案1】:

你得到了这个

Unexpected token o in JSON at position 1

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多