I've never used eval() before, so I assume that I just got the syntax horribly wrong. What's wrong with the following:
var JSONAsString = '{"item1":"one", "item2":"two", "item3":"three"}';
var JSONAsObject = eval(JSONString);
alert(JSONAsObject.item1);
Since it doesn't seem to be working - I load the page and nothing happens.
And yes, I know I shouldn't be using eval. I assume that the syntax for JSON.parse() is the same as that of eval... right? If it is, if (after fixing the code) I replace eval with JSON.parse, will it still do the same thing?
JSONStringvsJSONAsString,JSONAsObjectvsJSONObject- your variable names are incorrect. Besides that, after parsing/evaluating it it's a JavaScript object, not JSON (just to be nitpicking ;)), soJSONObjectdoesn't really fit.evaling the string results in a syntax error because the{...}are interpreted as block and not as object literal.Asin the variable name ;)