var x = {
"Item1" : 1,
"Item2" : {
"Item3" : 3
}
}
alert(JSON.stringify(x, undefined, 2));
alert($.parseJSON(x));
First one alerts valid object. The second one alerts null. In the real code, the “x” variable can be string or an object, so I should be able to parse both types. Am I missing something?
You’re parsing an object. You parse strings, not objects;
jQuery.parseJSONonly takes strings. From the documentation:Usage:
Standard jQuery doesn’t appear to have a JSON stringifier. Typically, jQuery handles that for you, so it isn’t necessary. If you need it, there are various plugins.