2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Parse JSON in JavaScript?

I want to parse a JSON string in JavaScript. Iam getting the following response

var response = "{"result":true,"count":1}"

I want to get the values of result and count?

userimage


You can find the easy way by using parse() method:

var response = '{"result":true,"count":1}';

var JsonObject= JSON.parse(response);


And you can get the values of the Json elements like :


var result = JsonObject.result;

var count= JsonObject.count;

userimage

try below :

jQuery.parseJSON(jsonString)
userimage

Most browsers support JSON.parse().

For other browsers use json2.js instead

You can do it the following way

var json = '{"result":true,"count":1}',

    obj = JSON.parse(json);

alert(obj.count);


If you're using jQuery, there is a $.parseJSON function that maps to JSON.parse if available or a form of eval in older browsers. However, this performs additional, unnecessary checks that are also performed by JSON.parse, so for the best performance use it in the following way.

var json = '{"result":true,"count":1}',

    obj = JSON && JSON.parse(json) || $.parseJSON(json);


This will ensure you use native JSON.parse immediately, rather than having jQuery perform sanity checks on the string before passing it to the native parsing function.

Answer is