Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析错误的JSON并能够显示错误所在_Javascript_Json_Node.js_Validation_Error Handling - Fatal编程技术网

Javascript 解析错误的JSON并能够显示错误所在

Javascript 解析错误的JSON并能够显示错误所在,javascript,json,node.js,validation,error-handling,Javascript,Json,Node.js,Validation,Error Handling,这不是关于如何管理或纠正错误的JSON,而是关于如何向用户解释错误JSON中的错误所在 有没有办法找出解析器在JSON中的哪个位置失败 我想在node.js应用程序中解决这个问题,所以如果可能的话,请将您的答案保存在该域中 当我对错误的JSON使用内置JSON对象和解析方法时,我只得到异常消息SyntaxError:Unexpected string。我想找出错误发生的地方 首选是返回结果ok/error和错误位置的JSON.validate(JSON)。大概是这样的: var faultyJs

这不是关于如何管理或纠正错误的JSON,而是关于如何向用户解释错误JSON中的错误所在

有没有办法找出解析器在JSON中的哪个位置失败

我想在node.js应用程序中解决这个问题,所以如果可能的话,请将您的答案保存在该域中

当我对错误的JSON使用内置JSON对象和解析方法时,我只得到异常消息
SyntaxError:Unexpected string
。我想找出错误发生的地方

首选是返回结果ok/error和错误位置的
JSON.validate(JSON)
。大概是这样的:

var faultyJsonToParse = '{"string":"value", "boolean": true"}';
var result = JSON.validate(faultyJsonToParse);
if (result.ok == true) {
   console.log('Good JSON, well done!');
} else {
   console.log('The validator found a \'' + result.error + '\' of type \'' + result.errorType + '\' in your JSON near position ' + result.position);
}
上述措施的预期结果将是:

The validator found a 'SyntaxError' of type 'Unexpected string' in your JSON near position 35.

JSON.parse()
的内置版本没有一致的行为。当论点形成良好时,它是一致的,如果不是,则是不一致的。这可以追溯到原始JSON库实现中该函数的不完整规范。规范不完整,因为它没有为异常对象定义接口。这种情况直接引出你的问题

虽然目前还没有现成的解决方案,但该解决方案需要编写JSON解析器并跟踪错误处理的位置信息。通过(1)首先调用本机版本,以及(2)如果本机版本引发异常,则调用位置感知版本(速度较慢),让它引发您自己的代码标准化的异常,可以将其无缝插入现有代码中。

尝试:

(尽管jsonLint是一个节点项目,但它也可以在web中使用:simply grab)

正如@eh9所建议的,围绕标准json解析器创建一个包装器来提供详细的异常信息是有意义的:

snippet (string): the actual line where the error happened
line (number)   : the line number of the error
column (number) : the column number of the error 
message (string): the parser's error message

如果您使用的是NodeJS,clarinet是一个非常好的基于事件的JSON解析器,它将帮助您生成更好的错误消息(行和列或错误)。 我已经使用以下方法构建了一个小型实用程序:


代码如下:

您是否希望在发生错误的地方将索引转换为一个缩小的JSON字符串?是的,类似这样。我想,这也可能是一个大型JSON中出现问题的途径。。也许你可以把这个通读一遍。。它可能无法回答您的问题,但可能会让您了解为什么本机不存在此功能。
Error: Parse error on line 1:
...ue", "boolean": true"}
-----------------------^
Expecting 'EOF', '}', ',', ']', got 'undefined'
JSON._parse = JSON.parse
JSON.parse = function (json) {
    try {
        return JSON._parse(json)
    } catch(e) {
        jsonlint.parse(json)
    }
}

JSON.parse(someJson) // either a valid object, or an meaningful exception
snippet (string): the actual line where the error happened
line (number)   : the line number of the error
column (number) : the column number of the error 
message (string): the parser's error message