当我 console.log
我的数据时,它在 Chrome 开发工具中看起来像这样:
e.LatLngBounds {_southWest: e.LatLng, _northEast: e.LatLng}
_northEast: e.LatLng
lat: 50.62507306341435
lng: -69.169921875
__proto__: Object
_southWest: e.LatLng
lat: 27.059125784374068
lng: -127.96875
__proto__: Object
__proto__: Object
如果我 JSON.stringify
它(像这样:var totbounds = JSON.stringify(mybounds);
),我得到这个:
{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}
我正在尝试解析它以获取 _southWest
lat
(例如),但这些都不起作用:
totbounds[_southWest][lat];
totbounds._southWest.lat;
(注意,totbounds
是字符串化对象)
最佳答案
JSON.stringify
将您的 JavaScript 对象转换为字符串,这样您就无法像您尝试的那样将其作为对象访问。字符串化的 JSON 不是您可以访问其属性的对象。该对象应保持对象状态,而不是字符串化。您的目的不需要它(如果我正确理解您的问题)。
给定:
var mybound = {_southWest: e.LatLng, _northEast: e.LatLng}
_northEast: e.LatLng
lat: 50.62507306341435
lng: -69.169921875
__proto__: Object
_southWest: e.LatLng
lat: 27.059125784374068
lng: -127.96875
__proto__: Object
__proto__: Object
要访问其属性,您可以通过:
var lat = mybound["_southWest"]["lat"];
关于javascript - 解析带有前导下划线的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769853/