Obviously I can do this unsafely with something like:
var obj = eval("(" + json + ')');
but that leaves me vulnerable to the JSON string containing other code, which it seems very dangerous to simply eval.
The jQuery method is now deprecated. Use this method instead:
let jsonObject = JSON.parse(jsonString);
Original answer using deprecated jQuery functionality:
If you’re using jQuery just use:
jQuery.parseJSON( jsonString );
It’s exactly what you’re looking for (see the jQuery documentation).
This answer is for IE < 7, for modern browsers check Jonathan’s answer above.
This answer is outdated and Jonathan’s answer above (
JSON.parse(jsonString)) is now the best answer.