Digging through some project code this evening, I ran across a line of jQuery similar to this:
jQuery.parseJSON(jQuery.parseJSON(json_string)));
I was curious as to why the calls were nested. Taking a closer look at the JSON string, I found that it contained slashes (apparently escaping the quotes - this is a PHP project). The JSON string is similar to this:
"[{\"input_index\": 0, \"secondary_index\": 0, \"street_address\": \"14106 Transportation Ave\", \"last_line\": \"Kennedy Building\"}]"
I separated the calls like so:
var res1 = jQuery.parseJSON(json_string);
var res2 = jQuery.parseJSON(res1);
I found that the first call produced another JSON string, with the slashes removed, similar to this:
[{"input_index": 0, "secondary_index": 0, "street_address": "14106 Transportation Ave", "last_line": "Kennedy Building"}]
The second call to jQuery.parseJSON actually produced a javascript object.
Why does this occur? I would expect the first call to fail.
The jQuery documentation here doesn't mention behavior like this. Granted, I'm still fairly new to jQuery, so I may be missing the obvious.
.parseJSONnot doing that it should be? Your output from the first call is valid JSON. It is also a valid object (not a string). See this fiddle. In one the keys are strings, the other plain variables. Both work the same way in JavaScript. In fact to be JSON, the strings (and keys) have to be surrounded by double quotes. Not sure what you're expecting. Please clarify...