I’m trying to decode a JSON with jQuery.
Here’s what I get (for instance a class, here with one student):
"{"Students":[{"Name":John,"Grade":17,}],"TotalClass":17,"TotalCount":1,}"
here’s what I do:
$j.ajax({
type: 'POST',
url: 'class.aspx/getClass',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
$j.each(msg, function (index, element) {
alert(element.TotalClass);
});
},
});
It keeps saying undefined in the alert (but I recieve the right JSON). Any idea what I’m doing wrong?
is not valid JSON !
Assuming you have a valid
JSONlike thisYou can access the values like this
Working sample : http://jsfiddle.net/ncbLF/5/
Use jsonlint to validate JSON
So your code can be simplified to