Solving the Invalid Label Error with JSON

So, while using JSON to create a String to parse with JavaScript/ECMAScript an error shows up "invalid label". Arrggg!!!

The basic code goes something like:

 var someJsonString = getDataFromServer();
 var myObj = eval(someJsonString);

The problem occurs because eval is interpreting the first item in the JSON string as a JavaScript Label. The solution is to wrap the JSON string in parenthesis.

 var someJsonString = getDataFromServer();
 var myObj = eval( "(" + someJsonString + ")" );

The errors are gone and life is good. I can read properties from the object. WooHoo!!!

References

una explicación en español de Walter Poch



Sponsors:

About willCode4Beer