PDA

View Full Version : json: eval



mickey
17th January 2008, 18:03
Hello,
I'm trying to convert a json var into a javascript object but when I write (in my file .html) the line relative to 'eval' the page doen't work and don't display anything:


<html>
<body>
<script type="text/javascript">
var myJSON = {"count":26,.......};
var myObject = eval("("+ myJSON+")"); //problem here
document.writeln(myJSON.value.title);
</script>
</body>
</html>

If I comment the 'eval' line, writeln print properly; otherwise it doesn't print!
Why eval doens't work, please?

Thanks.

jacek
17th January 2008, 18:39
I would bet on parse error. Check if your browser has any JavaScript console available --- it should display the error. myJSON isn't a string and you might need to call some function before you can put it inside () and pass to eval, however in this case there is no point in calling eval.

mickey
17th January 2008, 19:17
I don't understand..... I find this here (and on many other links):
http://www.json.org/js.html
my console say a strange thing:


Error: missing ] after element list on line 5
File out.html
([object Object])

line 5 is 'eval'
????

jacek
17th January 2008, 19:50
You need eval to change a string with data into an object, but in your example myJSON is already such object.

Try:
var myJSONtext = '{"count":26,.......}';
var myObject = eval( "("+ myJSONtext +")" );