javascript - JSON解析错误

标签 javascript json

我正在尝试从 Google 地方信息调用的结果中提取数据。

如果我运行以下 Javascript:

alert(JSON.stringify(places[0].geometry.location));

正如预期的那样,我得到以下输出:

{"lat":59.9138688,"lng":10.752245399999993}

但是,如果我将 .lat 放在末尾以提取 lat 值并运行:

alert(JSON.stringify(places[0].geometry.location.lat));

我得到:

undefined

如果我在没有 JSON.stringify 的情况下运行警报,如下所示:

alert(places[0].geometry.location.lat);

我得到:

function (){return a}

我做错了什么?

最佳答案

JSON.stringify - 是一个通常接受对象的函数(尽管它可以获取更多类型)。 在您的情况下,它需要一个数字,因为 places[0].geometry.location.lat 是一个数字而不是一个对象。

因此,如果你想获取一个字符串,你应该使用 Number 原型(prototype)附带的 .toString() 函数。

尝试 alert(places[0].geometry.location.lat.toString())

关于javascript - JSON解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40320922/

相关文章:

.net - 使用 Android 调用 .NET Webservice

javascript - 如何通过 Levenshtein 算法使用动态规划(在 Javascript 中)

javascript - Rails 4 - 使用脚本变量作为按钮值

javascript - 如何通过Javascript Jquery更改<a>的 "href"?

javascript - Ruby on Rails - gmaps4rails - 传递标记数据。超出最大调用堆栈大小

json - 将 Firebase 结果添加到数组时遇到问题

javascript - 为什么 {}__proto__ instanceof !== 对象

javascript - 在 Node.js 中实例化 javascript 类

python - 在python中以特定格式将json数组分割成更小的数组

arrays - 通过添加 JSonObject 创建 JsonArray