I want to parse a JSON string in JavaScript. The response is something like
var response = '{"result":true,"count":1}';
How can I get the values result
and count
from this?
I want to parse a JSON string in JavaScript. The response is something like
How can I get the values |
|||||
|
Most browsers support
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
This will ensure you use native |
|||||||||||||||||||||
|
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:
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. |
|||||||||||||
|
If you are getting this from an outside site it might be helpful to use jquery's getJSON. If it's a list you can iterate through it with $.each
|
|||
|
If you want to use JSON 3 for older browsers, you can load it conditionally with:
Now the standard window.JSON object is available to you no matter what browser a client is running. |
|||||||||||||||||
|
The following example will make it clear:
OR You can also use the
Since the |
||||
|
Without using a library you can use eg...
|
|||||||||||||||||
|
You can either use the eval function as in some other answers. (Don't forget the extra braces.) You will know why when you dig deeper), or simply use the jQuery function
OR You can use this below code.
And you can access the fields using |
|||||
|
If you pass a string variable (a well-formed jsonstring) to JSON.parse from MVC @Viewbag that has doublequote '"' as quotes, you need to process it before JSON.parse(jsonstring)
|
|||||||||||||
|
I thought |
|||||
|
As mentioned by numerous others, most browsers support Now, I'd also like to add that if you are using AngularJS (which I highly recommend), then it also provides the functionality that you require:
Just wanted to add the stuff about angularjs to provide another option. NOTE that angularjs doesn't officially support IE8 (and older versions, for that matter), though through experience most of the stuff seems to work pretty well. |
|||
|
JSON.parse() converts any JSON String passed into the function, to a JSON Object. For Better understanding press F12 to open Inspect Element of your browser and go to console to write following commands : -
Now run the command :-
you'll get output as Object {result: true, count: 1}. In order to use that Object, you can assign it to the variable let's say obj :-
Now by using obj and dot(.) operator you can access properties of the JSON Object. Try to run the command
|
||||
|
If you use jQuery, it is simple:
|
|||||||||
|
if you use dojo http://dojotoolkit.org/reference-guide/1.10/dojo/json.html require(["dojo/json"], function(JSON){ JSON.parse('{"hello":"world"}', true); }); |
|||
|
an easy way to do it
|
||||
|