i want to parse json to string in javascript. the response something like

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

how i can get the result value and count from this json

link|improve this question
2  
Your JSON isn't valid, did you mean to paste more of it? – Andy E Feb 8 '11 at 16:36
@andy as a mistake i do them right now see updated – user605334 Feb 8 '11 at 16:39
Search SO using "parse json javascript" and have a look at the abundance of questions and answers. – KooiInc Feb 8 '11 at 17:01
5  
@Kooilnc FYI, This question was the first result when I searched for "parse json javascript". – Zoot Aug 8 '11 at 14:25
possible duplicate of Serializing to JSON in jQuery – outis Dec 26 '11 at 10:10
feedback

3 Answers

up vote 30 down vote accepted

Most browsers support JSON.parse(), which is defined in ECMA-262 5th Edition (the specification that JS is based on). Its usage is simple:

var json = '{"result":true,"count":1}',
    obj = JSON.parse(json);

alert(obj.count);

For the browsers that don't you can implement it using json2.js.

As noted in the comments, if you're already 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 all round performance I'd recommend using it like so:

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.

link|improve this answer
thanks ........ – user605334 Feb 9 '11 at 8:32
i think JSON.parse not working in IE ?? – Marwan Nov 16 '11 at 9:26
4  
@Marwan: IE 8+ supports JSON.parse(). For IE 6, 7 and other older browsers, you can use the json2.js I linked to from my post. Alternatively, but less securely, you can use eval. – Andy E Nov 16 '11 at 9:46
@AndyE, you can also use jQuery too (in case it already included jQuery, he doesn't need an extra library.) – Derek May 8 at 23:25
1  
@Derek: yes, of course you can. However, to avoid jQuery's extra checks that are unnecessary when the JSON object is available, I'd suggest using something like obj = JSON && JSON.parse(str) || $.parseJSON(str). In fact, I may add that to the answer. – Andy E May 9 at 11:26
show 1 more comment
feedback

Without using a library you can use eval - the only time you should use. It's safer to use a library though.

eg...

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

var parsedJSON = eval('('+response+')');

var result=parsedJSON.result;
var count=parsedJSON.count;

alert('result:'+result+' count:'+count);
link|improve this answer
eval cant handle a json string which return as HTML – user192344 Mar 13 at 1:34
IF it's html-encoded it's not JSON anymore. – ThiefMaster May 9 at 11:20
feedback

First of all, you have to make sure that the JSON code is valid. After that, I would recommend using a JavaScript library such as jQuery or Prototype if you can because these things are handled well in those libraries. On the other hand, if you don't want to use a library and you can vouch for the validity of the JSON object, I would simply wrap string in an anonymous function and use the eval function. This is not recommended if you are getting the JSON object from another source that isn't absolutely trusted because the eval function allows for renegade code if you will. Here is an example of using the eval function:

var strJSON = '{"result":true,"count":1}';
var objJSON = eval("(function(){return " + strJSON + ";})()");
alert(objJSON.result);
alert(objJSON.count);

If you control what browser is being used or you are not worried people with older browser, you can always use the JSON.parse method. This is really the ideal solution for the future.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown