tags:

views:

45

answers:

2

Hi, everyone,

I have a question about the JSON. I am writing a JSON file, but I am not sure which of the following formats are correct. Can anyone help me?

{ "class":
    {
        "number": 2,
        "student": 
        {
        "name": "Tom",
        "age": 1
        },
        "student": 
        {
        "name": "May",
        "age": 2
        }
    }
}  

--------------------------

{ class:
    {
        number: 2,
        student: 
        {
        name: "Tom",
        age: 1
        },
        student: 
        {
        name: "May",
        age: 2
        }
    }
}  
+4  A: 

JSON requires the quotes. See http://json.org for the specifications.

In particular, the string production is:

string

""
" chars "

Greg Hewgill
+4  A: 

The first is valid, if you're unaware you can validate your JSON output online pretty easily here: http://www.jsonlint.com/

Nick Craver
or just try to `JSON.parse` it and see if you get a parse error.
MatrixFrog
@MatrixFrog - That won't always fail, depending on browser/implementation, it'll handle some invalid cases as well...what "works" and what's 100% valid often aren't the same thing :)
Nick Craver
That is disappointing to hear, but good to know.
MatrixFrog