Sunday, January 3, 2010

JSON String back to a Javascript Object invalid label error

I was pulling my hair out today, trying to turn a JSON string back into a Javascript Object using the "eval" function.

Turns out, you have add parenthesis to your JSON code.

Example would be:

var jsonString = Object.toJSON(jsonObject);

Where the jsonString would equal:

{ "id" : "jeffreystune", "name": "jeffreystune.blogspot.com"}

"id" would be an "invalid label" during an eval(jsonString);

The solution is simple, but hard to find:

var copyJsonObject = eval( "(" + jsonString +")" );

Ugh! Welcome to my geeky coding world.

~Jeffrey

No comments: