Results 1 to 4 of 4

Thread: json: eval

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default json: eval

    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:
    Qt Code:
    1. <html>
    2. <body>
    3. <script type="text/javascript">
    4. var myJSON = {"count":26,.......};
    5. var myObject = eval("("+ myJSON+")"); //problem here
    6. document.writeln(myJSON.value.title);
    7. </script>
    8. </body>
    9. </html>
    To copy to clipboard, switch view to plain text mode 
    If I comment the 'eval' line, writeln print properly; otherwise it doesn't print!
    Why eval doens't work, please?

    Thanks.
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: json: eval

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: json: eval

    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:
    Qt Code:
    1. Error: missing ] after element list on line 5
    2. File out.html
    3. ([object Object])
    To copy to clipboard, switch view to plain text mode 
    line 5 is 'eval'
    ????
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: json: eval

    You need eval to change a string with data into an object, but in your example myJSON is already such object.

    Try:
    Qt Code:
    1. var myJSONtext = '{"count":26,.......}';
    2. var myObject = eval( "("+ myJSONtext +")" );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. eval or better?
    By mikro in forum Newbie
    Replies: 6
    Last Post: 19th October 2006, 19:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.