Frage

For example, is this supposed to be a valid JSON document?

"foo"

The grammar specs at json.org isn't entirely clear. I don't think it is said anywhere in the specs that everything must be in a {} object or [] array in a valid JSON document.

JSONLint marks the stand-alone string "foo" as an error and expects everything to be inside a {} object or a [] array.

However, the JSON object of major browsers (IE 8, IE 10, Chrome 28, Firefox 23, Opera 12) accept stand-alone literals just fine:

>>> JSON.parse('"foo"');
"foo"
>>> JSON.parse('true');
true
>>> JSON.parse('1234');
1234

Same thing with Python 2.7+:

>>> import json
>>> json.loads('"foo"')
u'foo'
>>> json.loads('true')
True
>>> json.loads('1234')
1234

So who is right and who is wrong?

War es hilfreich?

Lösung

Found this on comment

Actually there are two different JSON specifications. RFC 4627 requires a JSON text to be an object or an array. ECMA-262, 5th edition, section 15.12 does not impose this restriction.

JSON root element

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top