What is the minimum valid JSON?

后端 未结 8 1948
慢半拍i
慢半拍i 2020-11-22 16:01

I\'ve carefully read the JSON description http://json.org/ but I\'m not sure I know the answer to the simple question. What strings are the minimum possible valid JSON?

8条回答
  •  广开言路
    2020-11-22 16:55

    According to the old definition in RFC 4627 (which was obsoleted in March 2014 by RFC 7159), those were all valid "JSON values", but only the last two would constitute a complete "JSON text":

    A JSON text is a serialized object or array.

    Depending on the parser used, the lone "JSON values" might be accepted anyway. For example (sticking to the "JSON value" vs "JSON text" terminology):

    • the JSON.parse() function now standardised in modern browsers accepts any "JSON value"
    • the PHP function json_decode was introduced in version 5.2.0 only accepting a whole "JSON text", but was amended to accept any "JSON value" in version 5.2.1
    • Python's json.loads accepts any "JSON value" according to examples on this manual page
    • the validator at http://jsonlint.com expects a full "JSON text"
    • the Ruby JSON module will only accept a full "JSON text" (at least according to the comments on this manual page)

    The distinction is a bit like the distinction between an "XML document" and an "XML fragment", although technically is a well-formed XML document (it would be better written as , but as pointed out in comments, the declaration is technically optional).

提交回复
热议问题