Python 搜索 JSON 中的错误

问题描述 投票:0回答:1

前提:那些字典是字符串。这些是格式错误的 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”:“山姆”“请”

“Ru”“tp”

“Ar”l”:{“名称”:“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

python json string python-2.7 format
1个回答
0
投票

您尝试过在 ANTLR4 中使用语法吗? 这是来自官方 antlr github 存储库的示例

© www.soinside.com 2019 - 2024. All rights reserved.