【问题标题】:JSON.parse syntax error while JSONLint tell me that json string is validJSON.parse 语法错误,而 JSONLint 告诉我 json 字符串有效
【发布时间】:2012-01-12 12:48:48
【问题描述】:

我使用 PHP 内置函数 json_encode() 将对象数组转换为 json string。然后我使用 AJAX 方法获取xmlhttp.responseText。我使用document.write(xmlhttp.responseText) 来检查响应字符串。例如,字符串如下:

{"index":0,"marks":2}

但是,当我使用 JSON.parse() 解析 JSON 字符串时,我得到一个语法错误。所以,我把这个字符串复制到JSONLint 来检查它是否有效,结果是否。

Parse error on line 1:
{    "index": 0
^
Expecting '{', '['

但是当我手动输入时,它是有效的。为什么?

【问题讨论】:

  • 这是什么? "index":0"index":"0"?

标签: php json validation parsing jsonlint


【解决方案1】:

您的 responseText 中可能有一些 zero-width whitespace character 或类似内容。

作为说明,我强烈建议使用console.log 而不是document.write 进行调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多