如何解析此非JSON数据?

时间:2018-09-30 04:57:55

标签: javascript python python-3.x data-structures scrapy-spider

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

3 个答案:

答案 0 :(得分:1)

我不知道您将如何在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进行上述操作,但是由于[(是需要在regex中转义的“特殊”字符,因此代码我认为还不清楚。

答案 1 :(得分:1)

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

如果括号{}[]() 可以出现在键或值字符串内,则您不能使用像这样的简单技术,需要使用适当的解析器。您可以从头开始编写解析器,也可以使用第三方库(例如str.maketrans)构建解析器。

答案 2 :(得分:0)

将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,  
 ...