I’m attempting to convert a JSON object to a “normal” object using the following…
var slaobj = eval('('+s+')');
s being the JSON. however, it doesnt seem to work (It’s `.length’ is coming back as undefined). What am I doing wrong?
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.
It won’t necessarily have a
lengthproperty, unless it’s an array or some other object that has one. For example:Live example
Off-topic: Using
evalto deserialize JSON text can be a security problem, unless you can unambiguously trust the source of the JSON text (for instance, it’s your own server and you’re connecting via SSL), becauseevaldoesn’t parse JSON, it parses and runs JavaScript code. (Adding the parentheses doesn’t really help.) You can get alternatives to usingevalfrom Douglas Crockford’s Github page (he’s the inventor of JSON). Last I checked, there are three alternatives there, two of which don’t useevalat all; see the README at the bottom of the page for details.