我正在尝试从 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/