如何解析此非 JSON 数据?

How can I parse this non-JSON data?

这是我的数据。它与 JSON 非常相似,但事实并非如此。现在我不知道如何在Python 3.

中使用它
(["icolor_ex": 0, "iDod_All": 91, "commu_gid": 0, "normal_horse": 0, "iBeastSki1": 0, "rent": 0, "datang_feat": 452, "total_avatar": 0, "iDef_All": 188, "iMag_All": 74, "iBadness": 0, "iLearnCash": 13205, "bid": 0, "iRes_All": 81, "iMp": 339, "iSchOffer": 0, "iSewski": 0, "more_attr": (["attrs": ({
    (["lv": 0, "idx": 1, ]),
    (["lv": 0, "idx": 2, ]),
    (["lv": 0, "idx": 8, ]),
    (["lv": 0, "idx": 12, ]),
    (["lv": 0, "idx": 7, ]),
    (["lv": 0, "idx": 6, ]),
    (["lv": 0, "idx": 11, ]),
    (["lv": 0, "idx": 4, ]),
    (["lv": 0, "idx": 5, ]),
    (["lv": 0, "idx": 3, ]),
    (["lv": 0, "idx": 9, ]),
    (["lv": 0, "idx": 10, ]),
    (["lv": 91, "idx": 13, ]),
    (["lv": 155, "idx": 14, ]),
}), ]), "HugeHorse": ([]), "iCGBoxAmount": 0, "iSumAmount": 3, "sword_score": 0, "all_skills": (["179": 1, "30": 45, "29": 45, "52032": 1, "34": 45, "25": 1, "52016": 1, "31": 45, "32": 45, "196": 1, "33": 1, "52031": 1, ]), "iPoint": 350, "iNutsNum": 0, "iOrgOffer": 0, "iMarry2": 0, "iExptSki2": 0, "sum_exp": 0, "iTotalMagDam_all": 155, "ExpJwBase": 1000000000, "iTotalMagDef_all": 155, "iSumAmountEx": 0, "idbid_desc": ({}), "cName": "Heler", "AllEquip": ([]), "iBeastSki4": 0, "AllSummon": ({}), "iExptSki4": 0, "iMaxExpt2": 20, "HeroScore": 0, "shenqi_pos": ({
    0,
    0,
}), "cOrg": "", "iPride": 733, "iHp_Max": 464, "iSpe_All": 79, "fabao": ([]), "iDamage_All": 107, "iMarry": 0, "outdoor_level": 0, "iCash": 32029, "shenqi": ([]), "iSkiPoint": 0, "iDex_All": 79, "iBeastSki2": 0, "usernum": 20759973, "iIcon": 9, "addPoint": 0, "commu_name": 0, "iDesc": 0, "iSchool": 7, "iGoodness": 0, "igoodness_sav": 0, "iStr_All": 80, "AchPointTotal": 91, "iCor_All": 81, "jiyuan": 0, "iCGBodyAmount": 0, "iAtt_All": 166, "iZhuanZhi": 0, "total_horse": 0, "iMagDef_All": 155, "iCGTotalAmount": 0, "iMp_Max": 339, "iSmithski": 0, "iPcktPage": 0, "iRace": 3, "TA_iAllPoint": 0, "iExptSki1": 0, "ori_desc": 146, "ExpJw": 0, "ExAvt": ([]), "i3FlyLv": 0, "rent_level": 0, "ori_race": 3, "iMaxExpt1": 20, "TA_iAllNewPoint": 0, "iBeastSki3": 0, "changesch": ({}), "AllRider": ([]), "iErrantry": 0, "iHp": 454, "iGrade": 69, "pet": ({}), "iMaxExpt3": 20, "iSaving": 0, "shenqi_yellow": "", "xianyu": 0, "iHp_Eff": 464, "energy": 0, "iMaxExpt4": 20, "iExptSki3": 0, "iUpExp": 1084477, "propKept": ([]), "farm_level": 0, "iExptSki5": 0, ])

我不知道您在 Python 中是怎么做的 - 但由于您标记了问题 javascript,您可以做的是获取该字符串并对其执行以下操作

var failedJson = `(["icolor_ex": 0, "iDod_All": 91, "commu_gid": 0, "normal_horse": 0, "iBeastSki1": 0, "rent": 0, "datang_feat": 452, "total_avatar": 0, "iDef_All": 188, "iMag_All": 74, "iBadness": 0, "iLearnCash": 13205, "bid": 0, "iRes_All": 81, "iMp": 339, "iSchOffer": 0, "iSewski": 0, "more_attr": (["attrs": ({
    (["lv": 0, "idx": 1, ]),
    (["lv": 0, "idx": 2, ]),
    (["lv": 0, "idx": 8, ]),
    (["lv": 0, "idx": 12, ]),
    (["lv": 0, "idx": 7, ]),
    (["lv": 0, "idx": 6, ]),
    (["lv": 0, "idx": 11, ]),
    (["lv": 0, "idx": 4, ]),
    (["lv": 0, "idx": 5, ]),
    (["lv": 0, "idx": 3, ]),
    (["lv": 0, "idx": 9, ]),
    (["lv": 0, "idx": 10, ]),
    (["lv": 91, "idx": 13, ]),
    (["lv": 155, "idx": 14, ]),
}), ]), "HugeHorse": ([]), "iCGBoxAmount": 0, "iSumAmount": 3, "sword_score": 0, "all_skills": (["179": 1, "30": 45, "29": 45, "52032": 1, "34": 45, "25": 1, "52016": 1, "31": 45, "32": 45, "196": 1, "33": 1, "52031": 1, ]), "iPoint": 350, "iNutsNum": 0, "iOrgOffer": 0, "iMarry2": 0, "iExptSki2": 0, "sum_exp": 0, "iTotalMagDam_all": 155, "ExpJwBase": 1000000000, "iTotalMagDef_all": 155, "iSumAmountEx": 0, "idbid_desc": ({}), "cName": "Heler", "AllEquip": ([]), "iBeastSki4": 0, "AllSummon": ({}), "iExptSki4": 0, "iMaxExpt2": 20, "HeroScore": 0, "shenqi_pos": ({
    0,
    0,
}), "cOrg": "", "iPride": 733, "iHp_Max": 464, "iSpe_All": 79, "fabao": ([]), "iDamage_All": 107, "iMarry": 0, "outdoor_level": 0, "iCash": 32029, "shenqi": ([]), "iSkiPoint": 0, "iDex_All": 79, "iBeastSki2": 0, "usernum": 20759973, "iIcon": 9, "addPoint": 0, "commu_name": 0, "iDesc": 0, "iSchool": 7, "iGoodness": 0, "igoodness_sav": 0, "iStr_All": 80, "AchPointTotal": 91, "iCor_All": 81, "jiyuan": 0, "iCGBodyAmount": 0, "iAtt_All": 166, "iZhuanZhi": 0, "total_horse": 0, "iMagDef_All": 155, "iCGTotalAmount": 0, "iMp_Max": 339, "iSmithski": 0, "iPcktPage": 0, "iRace": 3, "TA_iAllPoint": 0, "iExptSki1": 0, "ori_desc": 146, "ExpJw": 0, "ExAvt": ([]), "i3FlyLv": 0, "rent_level": 0, "ori_race": 3, "iMaxExpt1": 20, "TA_iAllNewPoint": 0, "iBeastSki3": 0, "changesch": ({}), "AllRider": ([]), "iErrantry": 0, "iHp": 454, "iGrade": 69, "pet": ({}), "iMaxExpt3": 20, "iSaving": 0, "shenqi_yellow": "", "xianyu": 0, "iHp_Eff": 464, "energy": 0, "iMaxExpt4": 20, "iExptSki3": 0, "iUpExp": 1084477, "propKept": ([]), "farm_level": 0, "iExptSki5": 0, ])`;

const obj = JSON.parse(failedJson
    .split(/\r?\n?/).join('') // make it a single line so things work
    .split('([').join('{') // convert ([ to {
    .split('])').join('}') // convert ]) to }
    .split('({').join('[') // convert ({ to [
    .split('})').join(']') // convert }) to ]
    .split(/,\s*}/g).join('}') // convert ,} to } because trailing , are invalid in JSON
    .split(/,\s*\]/g).join(']') // convert ,] to ] because trailing , are invalid in JSON
);
console.log(JSON.stringify(obj, null, 4));

您也可以使用 .replace 执行上述操作,但是因为 [( 是 "special" 个需要在 [=14= 中转义的字符], 在我看来,代码远没有那么清晰。

将 Jaromanda X 的代码转换为 Python 结果如下:

data = " ... your string here ... "

data = data.replace("\r", "")
data = data.replace("\n", "")
data = data.replace("\t", "")
data = data.replace("    ","")
data = data.replace("([", "{")
data = data.replace("])", "}")
data = data.replace("({", "[")
data = data.replace("})", "]")
data = data.replace(", }", "}")  
data = data.replace(", ]", "]")  

final_json = json.loads(data)

打印输出:

{'icolor_ex': 0,
 'iDod_All': 91,
 'commu_gid': 0,
 'normal_horse': 0,
 'iBeastSki1': 0,
 'rent': 0,
 'datang_feat': 452,
 'total_avatar': 0,
 'iDef_All': 188,  
 ...

如果方括号 {}[]() 仅用于分组并且 从未 出现在任何键或值字符串中,那么我们可以将该数据转换为一种形式可以被 ast.literal_eval. We just need to swap the square brackets and the braces, and remove the parentheses. That's easily done using str.translate to perform the translation and str.maketrans 解析以构建翻译 table。这比使用 str.replace 更快,因为它可以一步完成。

翻译完字符串数据后,我们使用 ast.literal_eval 将其转换为 Python 对象。我将使用 json.dumps 将 Python 对象转换为 JSON,这样我就可以很好地打印它。

from ast import literal_eval
import json

data = '''\
(["icolor_ex": 0, "iDod_All": 91, "commu_gid": 0, "normal_horse": 0,
"iBeastSki1": 0, "rent": 0, "datang_feat": 452, "total_avatar": 0,
"iDef_All": 188, "iMag_All": 74, "iBadness": 0, "iLearnCash": 13205,
"bid": 0, "iRes_All": 81, "iMp": 339, "iSchOffer": 0, "iSewski": 0,
"more_attr": (["attrs": ({
    (["lv": 0, "idx": 1, ]),
    (["lv": 0, "idx": 2, ]),
    (["lv": 0, "idx": 8, ]),
    (["lv": 0, "idx": 12, ]),
    (["lv": 0, "idx": 7, ]),
    (["lv": 0, "idx": 6, ]),
    (["lv": 0, "idx": 11, ]),
    (["lv": 0, "idx": 4, ]),
    (["lv": 0, "idx": 5, ]),
    (["lv": 0, "idx": 3, ]),
    (["lv": 0, "idx": 9, ]),
    (["lv": 0, "idx": 10, ]),
    (["lv": 91, "idx": 13, ]),
    (["lv": 155, "idx": 14, ]),
}), ]), "HugeHorse": ([]), "iCGBoxAmount": 0, "iSumAmount": 3,
"sword_score": 0, "all_skills": (["179": 1, "30": 45, "29": 45, "52032":
1, "34": 45, "25": 1, "52016": 1, "31": 45, "32": 45, "196": 1, "33": 1,
"52031": 1, ]), "iPoint": 350, "iNutsNum": 0, "iOrgOffer": 0, "iMarry2":
0, "iExptSki2": 0, "sum_exp": 0, "iTotalMagDam_all": 155, "ExpJwBase":
1000000000, "iTotalMagDef_all": 155, "iSumAmountEx": 0, "idbid_desc":
({}), "cName": "Heler", "AllEquip": ([]), "iBeastSki4": 0, "AllSummon":
({}), "iExptSki4": 0, "iMaxExpt2": 20, "HeroScore": 0, "shenqi_pos": ({
    0,
    0,
}), "cOrg": "", "iPride": 733, "iHp_Max": 464, "iSpe_All": 79, "fabao":
([]), "iDamage_All": 107, "iMarry": 0, "outdoor_level": 0, "iCash":
32029, "shenqi": ([]), "iSkiPoint": 0, "iDex_All": 79, "iBeastSki2": 0,
"usernum": 20759973, "iIcon": 9, "addPoint": 0, "commu_name": 0,
"iDesc": 0, "iSchool": 7, "iGoodness": 0, "igoodness_sav": 0,
"iStr_All": 80, "AchPointTotal": 91, "iCor_All": 81, "jiyuan": 0,
"iCGBodyAmount": 0, "iAtt_All": 166, "iZhuanZhi": 0, "total_horse": 0,
"iMagDef_All": 155, "iCGTotalAmount": 0, "iMp_Max": 339, "iSmithski": 0,
"iPcktPage": 0, "iRace": 3, "TA_iAllPoint": 0, "iExptSki1": 0,
"ori_desc": 146, "ExpJw": 0, "ExAvt": ([]), "i3FlyLv": 0, "rent_level":
0, "ori_race": 3, "iMaxExpt1": 20, "TA_iAllNewPoint": 0, "iBeastSki3":
0, "changesch": ({}), "AllRider": ([]), "iErrantry": 0, "iHp": 454,
"iGrade": 69, "pet": ({}), "iMaxExpt3": 20, "iSaving": 0,
"shenqi_yellow": "", "xianyu": 0, "iHp_Eff": 464, "energy": 0,
"iMaxExpt4": 20, "iExptSki3": 0, "iUpExp": 1084477, "propKept": ([]),
"farm_level": 0, "iExptSki5": 0, ])
'''

new_data = data.translate(str.maketrans('{}[]', '[]{}', '()'))
obj = literal_eval(new_data)
print(json.dumps(obj, indent=4))

输出

{
    "icolor_ex": 0,
    "iDod_All": 91,
    "commu_gid": 0,
    "normal_horse": 0,
    "iBeastSki1": 0,
    "rent": 0,
    "datang_feat": 452,
    "total_avatar": 0,
    "iDef_All": 188,
    "iMag_All": 74,
    "iBadness": 0,
    "iLearnCash": 13205,
    "bid": 0,
    "iRes_All": 81,
    "iMp": 339,
    "iSchOffer": 0,
    "iSewski": 0,
    "more_attr": {
        "attrs": [
            {
                "lv": 0,
                "idx": 1
            },
            {
                "lv": 0,
                "idx": 2
            },
            {
                "lv": 0,
                "idx": 8
            },
            {
                "lv": 0,
                "idx": 12
            },
            {
                "lv": 0,
                "idx": 7
            },
            {
                "lv": 0,
                "idx": 6
            },
            {
                "lv": 0,
                "idx": 11
            },
            {
                "lv": 0,
                "idx": 4
            },
            {
                "lv": 0,
                "idx": 5
            },
            {
                "lv": 0,
                "idx": 3
            },
            {
                "lv": 0,
                "idx": 9
            },
            {
                "lv": 0,
                "idx": 10
            },
            {
                "lv": 91,
                "idx": 13
            },
            {
                "lv": 155,
                "idx": 14
            }
        ]
    },
    "HugeHorse": {},
    "iCGBoxAmount": 0,
    "iSumAmount": 3,
    "sword_score": 0,
    "all_skills": {
        "179": 1,
        "30": 45,
        "29": 45,
        "52032": 1,
        "34": 45,
        "25": 1,
        "52016": 1,
        "31": 45,
        "32": 45,
        "196": 1,
        "33": 1,
        "52031": 1
    },
    "iPoint": 350,
    "iNutsNum": 0,
    "iOrgOffer": 0,
    "iMarry2": 0,
    "iExptSki2": 0,
    "sum_exp": 0,
    "iTotalMagDam_all": 155,
    "ExpJwBase": 1000000000,
    "iTotalMagDef_all": 155,
    "iSumAmountEx": 0,
    "idbid_desc": [],
    "cName": "Heler",
    "AllEquip": {},
    "iBeastSki4": 0,
    "AllSummon": [],
    "iExptSki4": 0,
    "iMaxExpt2": 20,
    "HeroScore": 0,
    "shenqi_pos": [
        0,
        0
    ],
    "cOrg": "",
    "iPride": 733,
    "iHp_Max": 464,
    "iSpe_All": 79,
    "fabao": {},
    "iDamage_All": 107,
    "iMarry": 0,
    "outdoor_level": 0,
    "iCash": 32029,
    "shenqi": {},
    "iSkiPoint": 0,
    "iDex_All": 79,
    "iBeastSki2": 0,
    "usernum": 20759973,
    "iIcon": 9,
    "addPoint": 0,
    "commu_name": 0,
    "iDesc": 0,
    "iSchool": 7,
    "iGoodness": 0,
    "igoodness_sav": 0,
    "iStr_All": 80,
    "AchPointTotal": 91,
    "iCor_All": 81,
    "jiyuan": 0,
    "iCGBodyAmount": 0,
    "iAtt_All": 166,
    "iZhuanZhi": 0,
    "total_horse": 0,
    "iMagDef_All": 155,
    "iCGTotalAmount": 0,
    "iMp_Max": 339,
    "iSmithski": 0,
    "iPcktPage": 0,
    "iRace": 3,
    "TA_iAllPoint": 0,
    "iExptSki1": 0,
    "ori_desc": 146,
    "ExpJw": 0,
    "ExAvt": {},
    "i3FlyLv": 0,
    "rent_level": 0,
    "ori_race": 3,
    "iMaxExpt1": 20,
    "TA_iAllNewPoint": 0,
    "iBeastSki3": 0,
    "changesch": [],
    "AllRider": {},
    "iErrantry": 0,
    "iHp": 454,
    "iGrade": 69,
    "pet": [],
    "iMaxExpt3": 20,
    "iSaving": 0,
    "shenqi_yellow": "",
    "xianyu": 0,
    "iHp_Eff": 464,
    "energy": 0,
    "iMaxExpt4": 20,
    "iExptSki3": 0,
    "iUpExp": 1084477,
    "propKept": {},
    "farm_level": 0,
    "iExptSki5": 0
}

如果括号{}[]() 可以出现在键或值字符串中,那么你不能使用这样简单的技术,你需要使用适当的解析器。您可以从头开始编写一个解析器,或者您可以使用第 3 方库构建一个解析器,例如 pyparsing.