Python中查找JSON错误

-1 投票
1 回答
32 浏览
提问于 2025-04-12 10:07

前提:这些字典是字符串。它们是格式不太好的JSON。我在这些JSON里面查找错误并打印出来。

例子1:

input

{
    "ced": {
        "CED_PH2": "_VOID_",
        "CED_PH": "_VOID_",
        "CED_IR": "_VOID_"
    },
    "shh": {
        "An": {
            "name": "c",
            "ines": "Sam " "ples",
            "in": "bu"   
        },
        "Ar"l": {
            "name": "uu",
            "i": "aa",   
        },
        "At": {
            "name": "Ru" "tp",
            "inute": "Ae",
            "intColor": "Dlor",
            "tal": "tal"
        },
        "We": {
            "name": "Wue",
            "ior": "Pour",
            "iss": "Wus"
        }
    }
}

我希望它能显示出以下错误:

"ines": "Sam " "ples"

"Ru" "tp"

"Ar"l": {"name": "uu" 或者 "Ar"l"

我还想从

input 
{
       "CED_PHS": "_VOID_",
       "CED_PH": "_VOI""D_",
       "CED_IR": "_VO"ID_",
       "name": "Wue",
           "ior": "Pour",
           "iss": "Wus"
}

显示出以下错误:

"CED_PH": "VOI""D",

"CED_IR": "VO"ID",

我的代码是:

def writeProblems(content):
    print(content)
    start_index = content.find('{') + 1  
    end_index = content.rfind('}') 
    if start_index > 0 and end_index > 0:
        content = content[start_index:end_index]  
    lines = content.split(',\n')
    counter = 0
    pattern = r'^\s*"[^"]*"\s*:\s*"[^"]*"\s*$'
    pattern2 =  r'\s*"[^"]+"(?=:)\s*:\s*{(?:\s*"[^"]+"\s*:\s*{(?:\s*"[^"]+"\s*:\s*{(?:\s*"[^"]+"\s*:\s*"[^"]*"\s*,?\s*)*}\s*,?\s*)*}\s*,?\s*)*}\s*'


    for line in lines:
        counter += 1
        if not re.match(pattern, line) and not re.match(pattern2, line):
            print('line %s: has a " in the middle --->%s<---' % (counter, line))
    return False

1 个回答

-1

你有没有试过用ANTLR4来处理语法呢?
这里有一个例子,来自ANTLR的官方GitHub库,展示了如何处理JSON格式的数据。

撰写回答