开发者

Why does JSON.parse('{"key" : "value"}') do just fine but JSON.parse('{key : "value"}') doesn't?

From the node REPL:

> JSON.parse('{"key" : "value"}')
{ key: 'value' }
> JSON.parse('{key : "value"}')
SyntaxError: Unexpected token ILLEGAL
    at Object.parse (native)
    at [object Context]:1:6
    at Interface.<anonymous> (repl.js:171:22)
        at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Int开发者_高级运维erface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)


All keys in JSON are strings, and all strings are delimited by double quotes.

JavaScript allows you to use different characters to delimit strings, and it allows you to use identifiers as keys in an object literal — but JSON is not JavaScript.


JSON expects both keys and values to be strings.

http://www.json.org/js.html


Because your second example isn't JSON.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜