簡體   English   中英

如何打印 json 文件的特定行號

[英]How can i print the specific line number of a json file

我正在使用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' is a required property ,即error.message

我想要打印: On line number 4 ,'lending_details' is a required property

有沒有辦法計算和顯示json文件的特定行號?

一般來說,不會,因為當 JSON Schema 評估器看到數據實例時,數據已經從 JSON 文本解析為數據結構,並且行號信息已經丟失。

要完成這項工作,您將需要一個 JSON 解碼器,該解碼器可以將行號與數據的每個部分相關聯,以便 JSON 模式評估器稍后在生成錯誤時可以使用它。 例如,我可以看到一個解碼器正在轉換這個 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”處生成錯誤時,我們可以使用此查找表將“..at line 8”插入錯誤中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM