I try to change a string into an object using eval but failed.
var obj ="{a:0, b:-1}";
eval(obj);
Error msg says “invalid label” but even this doesn’t work
var obj="{'a':'0', 'b':'-1'}";
What’s wrong with the code?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
when
evaling json, you have to put braces around it, so it should look like (otherwise it’s not a complete javascript-statement):this solved the error, but the generated object isn’t saved to any variable – so you might want to end up with something else, like:
this also makes a complete statement and, in addition,
objnow is a “real” object.