user5407287 user5407287 - 5 months ago 9
JSON Question

Converting JSON to Usable object in Javascript

I have some JSON with two parameters expressed as

{"pushid":"35336165333161382d646338302d346665302d626236312d303763313435663036656131","count":1}


I'm trying to get access to the "pushid" and the "count" as usable elements, either in an object, an array or a map, and am a little confused as to how to do that.

When I call JSON.parse(json) it returns undefined, and so I assume that it's already an object. However, when I try to use json[1] it returns the second character of the whole thing (which in this case is "). How do I make an object

var obj = {pushId: SOME_STRING, count: SOME_INT)?


Thanks in advance,

Answer

Considering:

var jsonString = '{"pushid":"35336165333161382d646338302d346665302d626236312d303763313435663036656131","count":1}';

You can do:

var jsonObj = JSON.parse(jsonString);
console.log(jsonObj.pushid); // 3533...
console.log(jsonObj['pushid']); // 3533...
console.log(jsonObj.count); // 1
console.log(jsonObj['count']); // 1
console.log(jsonObj[0]); // undefined
console.log(jsonObj[1]); // undefined