javascript - Error when adding a new member to JSON -


(i checked out @ least couple dozen posts on error , made few changes see fit no avail)

so, started out simple var:

var _jsonstr = '{"hotspots:":[]}'; 

then, parsed it:

var _jsonobj = json.parse(_jsonstr); 

in other part of html, values variable id, x, , y , assign values _jsonobj follows:

_jsonobj.hotspots[0].id = id;   // error! _jsonobj.hotspots[0].xval = x; _jsonobj.hotspots[0].yval = y; 

and i'd end set of id, x, y value pairs in json like:

var _jsonobj = {     "hotspots": [         { id: 0, xval: 25, yval: 50 },         { id: 1, xval: 80, yval: 120 },         { id: 2, xval: 39, yval: 91 },         ...     ] }; 

hate admit couldn't figure out why keep getting error says, "unable set property 'id' of undefined or null reference" commented above. sounds me doing wrong adding new member json object don't see why so.

you getting error because _jsonobj.hotspots[0] undefined.

this should fix it:

_jsonobj.hotspots[0] = {}; _jsonobj.hotspots[0].id = id;   _jsonobj.hotspots[0].xval = x; _jsonobj.hotspots[0].yval = y; 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -