代码之家  ›  专栏  ›  技术社区  ›  pythonjsgeo

JSON.parse(null)和JSON.parse(false)不应该抛出异常吗?

  •  0
  • pythonjsgeo  · 技术社区  · 6 年前

    parse(…)的参数应该是有效的JSON字符串,但null和false不是字符串。

    注意,我正在传递javascript 不是“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中测试)

    1 回复  |  直到 6 年前
        1
  •  4
  •   deceze    6 年前

    JSON.parse explicitly casts the argument to a string . null 字符串中的结果 'null' false 在字符串中 'false' .