What is the minimum valid JSON?

后端 未结 8 1946
慢半拍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条回答
  •  猫巷女王i
    2020-11-22 16:50

    The ecma specification might be useful for reference:

    http://www.ecma-international.org/ecma-262/5.1/

    The parse function parses a JSON text (a JSON-formatted String) and produces an ECMAScript value. The JSON format is a restricted form of ECMAScript literal. JSON objects are realized as ECMAScript objects. JSON arrays are realized as ECMAScript arrays. JSON strings, numbers, booleans, and null are realized as ECMAScript Strings, Numbers, Booleans, and null. JSON uses a more limited set of white space characters than WhiteSpace and allows Unicode code points U+2028 and U+2029 to directly appear in JSONString literals without using an escape sequence. The process of parsing is similar to 11.1.4 and 11.1.5 as constrained by the JSON grammar.

    JSON.parse("string"); // SyntaxError: Unexpected token s
    JSON.parse(43); // 43
    JSON.parse("43"); // 43
    JSON.parse(true); // true
    JSON.parse("true"); // true
    JSON.parse(false);
    JSON.parse("false");
    JSON.parse("trueee"); // SyntaxError: Unexpected token e
    JSON.parse("{}"); // {}
    JSON.parse("[]"); // []
    

提交回复
热议问题