最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse JSON with leading underscore - Stack Overflow

programmeradmin2浏览0评论

When I console.log my data, it looks like this in Chrome Dev Tools:

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

If I JSON.stringify it (like so: var totbounds = JSON.stringify(mybounds);), I get this:

{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}

I'm trying to parse it to get the _southWest lat (for example), but neither of these work:

totbounds[_southWest][lat];
totbounds._southWest.lat;

(Note, totbounds is the stringified object)

When I console.log my data, it looks like this in Chrome Dev Tools:

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

If I JSON.stringify it (like so: var totbounds = JSON.stringify(mybounds);), I get this:

{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}

I'm trying to parse it to get the _southWest lat (for example), but neither of these work:

totbounds[_southWest][lat];
totbounds._southWest.lat;

(Note, totbounds is the stringified object)

Share Improve this question asked Jun 11, 2015 at 0:59 jonmrichjonmrich 4,3338 gold badges51 silver badges96 bronze badges 4
  • 3 If you stringify the object, you get a JSON string, not an object. You should read the properties from the object, not the string. Use mybounds._southWest.lat. – Guffa Commented Jun 11, 2015 at 1:04
  • Try this: jsbin./lumeqemaza/1/watch?js,console – Chan Commented Jun 11, 2015 at 1:04
  • 1 I think you want mybounds._southWest.lat or mybounds['_southWest']['lat'] – Phil Commented Jun 11, 2015 at 1:06
  • How did you parse it? – The_Black_Smurf Commented Jun 11, 2015 at 1:06
Add a ment  | 

2 Answers 2

Reset to default 5

JSON.stringify converts your JavaScript object to string so you can't access it as object like you tried. Stringified JSON is not an object you can access its property. The object should remain object, not stringified. You don't need it in your purpose (if I understand your problem correctly).

Given:

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

To access its property, you can do via:

var lat = mybound["_southWest"]["lat"];    

If mybounds is your object, you can just do

mybounds._southWest.lat;

Example:

var str = '{"_southWest":{"lat":27.059125784374068,"lng":-127.96875},"_northEast":{"lat":50.62507306341435,"lng":-69.169921875}}';

var mybounds= JSON.parse(str);
console.log(mybounds._southWest.lat);

27.059125784374068

发布评论

评论列表(0)

  1. 暂无评论