gpt4 book ai didi

json - 获取 json 验证失败的 json 文件的行号

转载 作者:行者123 更新时间:2023-12-02 04:50:47 25 4
gpt4 key购买 nike

我正在使用 json-schema-validator用于验证我的 json。

我想显示 json 数据文件中发生验证失败的行号。我想以用户友好的方式显示失败消息。我得到指向可能发生验证失败的 json 节点的指针,如下所示:

JsonNode jsondatanode = JsonLoader.fromFile(new File("jsondata.json"));
JsonNode jsonschemanode = JsonLoader.fromFile(new File("jsonschema.json"));

final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema datastoreschema = factory.getJsonSchema(jsonschemanode);

ProcessingReport report;
report = datastoreschema.validate(jsondatanode);

但是当json文件包含很多指针指定类型的节点时,指针不方便定位json对象/属性。

我收到以下验证失败消息:

--- BEGIN MESSAGES ---
error: instance value (12) not found in enum (possible values:["true","false","y","n","yes","no",0,1])
level: "error"
schema: {"loadingURI":"#","pointer":"/properties/configuration/items/properties/skipHeader"}
instance: {"pointer":"/configuration/0/skipHeader"}
domain: "validation"
keyword: "enum"
value: 12
enum: ["true","false","y","n","yes","no",0,1]
--- END MESSAGES ---

我想在 json 数据文件中显示验证失败的自定义消息以及导致模式验证失败的行号。我知道我可以访问验证报告的各个详细信息,如下面的代码所示。

我想显示自定义消息如下:

List<ProcessingMessage> messages = Lists.newArrayList((AbstractProcessingReport)report);
JsonNode reportJson = messages.get(0).asJson();
if(reportJson.get("keyword").toString().equals("enum"))
{
System.out.println("Value "+report.Json.get("value").toString() +"is invalid in " + filepath + " at line " + linenumber);
}
else if{
//...
}
//...

我不明白的是如何在上面的代码中获取 linenumber 变量。

编辑

现在我明白了

instance: {"pointer":"/configuration/0/skipHeader"}

显示 skipHeader 出现的问题,在本例中是 configurationskipHeader 的第 0 个实例。但是我仍然认为最好获取遇到问题的行号。

最佳答案

(图书馆作者在这里)

虽然可以做到(我在某个地方有一个 JsonParser 的实现,它就是这样做的)问题是行/列信息在大多数情况下是无关紧要的。

为了节省带宽,大多数时候,通过网络发送的 JSON 将始终在单行上,因此问题仍然存在,例如,您会得到“第 1 行,第 202 列”而没有得到任何更聪明。

我可能会在下一个主要版本中这样做,但对于 2.2.x 来说已经太晚了......

关于json - 获取 json 验证失败的 json 文件的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28762093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com