user5407287 user5407287 - 1 year ago 67
JSON Question

Converting JSON to Usable object in Javascript

I have some JSON with two parameters expressed as


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 Source


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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download