如何打印JSON文件的特定行号

发布于 2025-02-09 03:35:20 字数 516 浏览 2 评论 0原文

我正在使用python加载JSON文件并使用JSonschema根据我准备的schema打印错误。

我的问题是如何从循环中打印JSON文件的特定行:

errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
for error in errors:
    print(error.message, sep=", ")

我获得的输出是'lending_details'是必需的属性,它是error.message

我想要打印的是:在行编号4,'Lending_details'是必需的属性

是否有一种方法来计数并显示特定行号JSON文件?

I am using python to load a json file and using jsonschema to print errors according to the schema i have prepared.

My question is how do i print a specific line of a json file from a loop:

errors = sorted(validator.iter_errors(jsonData[a]), key=lambda e: e.path)
for error in errors:
    print(error.message, sep=", ")

The output i get is 'lending_details' is a required property which is the error.message.

What i want is to print: On line number 4 ,'lending_details' is a required property

Is there a way to count and display the specific line number of a json file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2025-02-16 03:35:20

通常,不,因为到JSON架构评估器看到数据实例时,数据已从JSON文本解析为数据结构,并且丢失了行编号信息。

为了完成这项工作,您需要拥有一个JSON解码器,该解码器可以以JSON Schema评估器以后可以在生成错误时使用它的方式将行号与数据的每个部分相关联。例如,我可以看到一个解码器将此JSON转换为:

{
  "foo": {
    "hello": [
      "a",
      "b",
      "c"
    ]
  "bar": true
}

进入此行编号映射器:

{
  "": 1,
  "/foo": 2,
  "/foo/hello": 3,
  "/foo/hello/0": 4,
  "/foo/hello/1": 5,
  "/foo/hello/2": 6,
  "/bar": 8
}

..然后,当JSON Schema评估器正在在数据实例“/bar”上生成错误时,我们可以使用此查找表来插入。在第8行中陷入错误。

In general, no, because by the time the JSON Schema evaluator sees the data instance, the data has been parsed from JSON text into a data structure and the line number information has been lost.

To make this work, you will need to have a JSON decoder that can associate line numbers with each section of the data in a way that the JSON Schema evaluator can later make use of it when generating its errors. For example, I could see a decoder turning this JSON:

{
  "foo": {
    "hello": [
      "a",
      "b",
      "c"
    ]
  "bar": true
}

into this line number mapper:

{
  "": 1,
  "/foo": 2,
  "/foo/hello": 3,
  "/foo/hello/0": 4,
  "/foo/hello/1": 5,
  "/foo/hello/2": 6,
  "/bar": 8
}

..and then when the JSON Schema evaluator is generating an error at data instance "/bar", we can use this lookup table to insert "..at line 8" into the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文