gpt4 book ai didi

javascript - 我如何解析这个非 JSON 数据?

转载 作者:行者123 更新时间:2023-11-29 20:49:38 28 4
gpt4 key购买 nike

这是我的数据。它与 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, ])

最佳答案

如果括号 {}[]() 仅用于分组并且从不出现在任何键或值字符串中,那么我们可以将该数据转换为ast.literal_eval 可以解析的表单.我们只需要交换方括号和大括号,并删除括号。使用 str.translate 很容易做到这一点执行翻译和str.maketrans建立翻译表。这比使用 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 .

关于javascript - 我如何解析这个非 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574777/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com