JS: eval json
var jsonEmployee = "{ name: 'Ray', age: 31 }";
var employee = eval(jsonEmployee);
script error: sun.org.mozilla.javascript.internal.EcmaError: SyntaxError: missing ; before statement (<STDIN>#1(eval)#1) in <STDIN>#1(eval) at line number 1
The problem that "{ ... }"
is a block of code. So if we wants to have a json we need to add []
, i.e. "[{ ... }]"
or use expressions "([ ... ])"
Note that valid JSON need quoted items names.
{ "name": "Ray", "age": 31 }
Comments
comments powered by Disqus