This is a JSON code that is generated on the server side and sent through websockets to the client as a string:
{
"zw_ob-k7g8ahcvFzAAAA":{
"position":{
"x":319,
"y":135
},
"mass":324,
"name":21,
"color":"#5058a9"
},
"uTFD8dw0OiqD4ErzAAAB":{
"position":{
"x":473,
"y":348
},
"mass":59,
"name":7,
"color":"#e79448"
},
"CvMRJ6rEFYoU1vajAAAC":{
"position":{
"x":25,
"y":604
},
"mass":147,
"name":18,
"color":"#cef6c3"
},
"_e6TLrfubHqf-7esAAAD":{
"position":{
"x":146,
"y":417
},
"mass":320,
"name":13,
"color":"#ab7aa0"
}
}
On the client I use JSON.Parse to parse this string into json structure.
planetsData = JSON.parse(data.planets);
for(var p in planetsData){
var playerName = p;
alert(p.position) //says "undefined"
var planetColor = p.color;
var planetName = p.name;
var planet = new Planet();
}
I can successfully get p, p.color and p.name, but for some reason it says that p.position is undefined, hence I can't access p.position.x or p.position.y parameters.
console.log(p)?pis the key and not the complete objectplanetsData[p].position