JSON.parse(...) 的参数应该是一个有效的 json 字符串,但 null 和 false 不是字符串。
请注意,我传递的是 javascript null 而不是“null”(作为字符串)。显然 JSON.parse('null') 应该可以正常工作。
JSON.parse(null) returns null?!
JSON.parse(false) returns null?!
他们不应该抛出异常吗?
JSON.parse(undefined) throws exception, as expected
JSON.parse('') throws exception, as expected
(在最近的 Chrome 和 Firefox 中测试过)
最佳答案
JSON.parse
explicitly casts the argument to a string . null
在字符串 'false'
中产生字符串 'null'
和 false
。 null
和 false
是有效的 JSON 值,因此它们可以很好地解码。
关于javascript - JSON.parse(null) 和 JSON.parse(false) 不应该抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53095713/