I have the following JSON returned from an AJAX call:
{"rows":[{"Date":"07/10/2011","Value":1206,"Action":"Drink"},
{"Date":"07/11/2011","Value":2288,"Action":"Pie"},
{"Date":"07/12/2011","Value":1070,"Action":"Drink"},
{"Date":"07/13/2011","Value":1535,"Action":"Beer"},
{"Date":"07/14/2011","Value":1721,"Action":"Drink"}],
"page":1,"total":1,"records":5}
How can I get values from this result using jQuery?
For instance, I'd like to get data elements for the first row:
json.rows[0].date = 07/10/2011
json.rows[0].value = 1206
?
There are a number of ways to do this, you could use eval() but eval() is generally considered evil, plus JSON is not exactly the same as a defining data in Javascript code. I just googled which reminded me that jquery has a parseJSON function.
Check it out.
http://api.jquery.com/jQuery.parseJSON/
But if you are using jquery for the ajax ( pretty popular ), you can set the dataType to json and this will do it for you.
$.ajax(
{
url: "/map", // should return JSON
dataType: 'json',
cache: false,
data: {},
success: function(response)
{
// response is an object
alert( response.rows[0].Date );
cook( response );
}
});