Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're taking a JSON string and pretending that it's a Javascript object literal. But, remember, <em>JSON is merely <strong>named after</strong> Javascript's object notation!</em></p> <p>You're then hoping that <code>eval</code> will directly evaluate this and return an object, but this is not the case.</p> <hr> <h3>Observations</h3> <p>Writing the following in the Firebug console:</p> <pre><code>eval('{"caption":"A caption","filename":"myimage_thumb.jpg"}') </code></pre> <p>will also yield a SyntaxError. (Ideally, you'd have narrowed your problem down to this!)</p> <p>Interestingly, constructing an object more explicitly does work (for this input):</p> <pre><code>eval('new Object({"caption":"A caption","filename":"myimage_thumb.jpg"})') </code></pre> <p>As does just forcing the input into being a certain kind of expression, with parentheses:</p> <pre><code>eval('({"caption":"A caption","filename":"myimage_thumb.jpg"})') </code></pre> <hr> <h3>Explanation</h3> <p>It all comes down to this statement from 12.4 in the ECMAScript standard:</p> <blockquote> <p>Note that an <code>ExpressionStatement</code> cannot start with an opening curly brace because that might make it ambiguous with a Block.</p> </blockquote> <p><a href="http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-json-object-object-literal/" rel="noreferrer">This article</a> explains the issue in depth, but in summary <strong>the solution is to use <code>JSON.parse(res)</code> to obtain an object from a JSON string</strong>. This is the proper approach, and it will not fail as long as your input is valid JSON.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload