> > From: Sam Sherlock
> >
> > my object looks like this:
> > {'title':'credits', 'content':'    <div id="credits"><h2>Credits</h2>
> > <div><p>Content</p></div></div>', 'image':'<img
src="assets/images/scans/oink.jpg"
> > width="467" height="750" alt="" />', 'music':8}
> > 
> > also should I have quotes around the prop name aswell as 
> > the value of the prop? some example do and some don't
> > maybe this is optional?

> From: Klaus Hartl
> 
> As per JSON definition[1], the property names must be 
> strings, you therefore should leave them wrapped in quotes. I 
> also read somewhere that otherwise you can run into some 
> strange problems (cannot find it anywhere right now).
> 
> The property values can be one of the JavaScript data types
> 
> string
> number
> object
> array
> boolean
> null
> 
> i.e. only use quotes if you have a string.
>
> [1] http://json.org/

And, to be JSON, all the quoted strings need to use "double quotes". The
JSON standard doesn't allow 'single quotes'.

The reason it hasn't caused any problem is that you're not using a JSON
parser to parse the JSON text, you're using eval(). And eval() of course
will accept (among other things) any JavaScript object literal.

Object literal notation allows either type of quote, and it makes the quotes
optional on property names as long as the name is a valid JavaScript
identifier. JSON is a subset of object literal notation and it is stricter
about the quotes.

Since you're using eval(), it doesn't really matter in a practical sense
whether you use strict JSON format or not. After all, even though we're
calling it "JSON", we're really using JavaScript code. You could even put
functions and stuff in there - download an object and the methods to go with
it.

You'll need to use the exact JSON format if you ever run your JSON data
through an actual JSON parser like the ones listed on json.org.

As an example, either of these would be valid JSON for your credits object:

   {
      "title": "credits",
      "content": "    <div
id=\"credits\"><h2>Credits</h2><div><p>Content</p></div></div>",
      "image": "<img src=\"assets/images/scans/oink.jpg\" width=\"467\"
height=\"750\" alt=\"\" />",
      "music": 8
   }

or:

   {
      "title": "credits",
      "content": "    <div
id='credits'><h2>Credits</h2><div><p>Content</p></div></div>",
      "image": "<img src='assets/images/scans/oink.jpg' width='467'
height='750' alt='' />",
      "music": 8
   }

Typically that would all be one one line with the extra whitespace removed,
but it's valid with or without the whitespace.

-Mike


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to