我有一个下面的json data。
object 'parents‘的结构是:
parents:[{
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Tixaquir",
"race": "LUSITANO",
"parents": [{
...same as above ...,
"parents" : [{},{}]
},
{
...same as above ...,
"parents" : [{},{]]
}]
},
{
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Tixaquir",
"race": "LUSITANO",
"parents": [{},{}]
}]我想把每个对象的“parents”列表分解为
sire = js['parents'][0]
dam = js['parents'][1]而是针对项“parents”的所有级别(缩进)。因此,该对象将如下所示:
'sire' : {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Tixaquir",
"race": "LUSITANO",
"sire":{
...same as above ...,
"sire" :{},
"dam" :{}
},
"dam" :{
...same as above...,
"sire" :{},
"dam" :{}
}
},
'dam': {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Tixaquir",
"race": "LUSITANO",
"sire": {},
"dam" : {}
}我正在努力解决的问题是一个链式项目“parents”。我尝试了一些迭代从json.loads(js.decode("utf-8"))返回的python字典,但总是失败。我正在使用python 3,提前谢谢!
发布于 2020-06-22 05:08:08
我希望我正确理解了您的问题:您希望用{'sire':..., 'dam':...}结构替换每个'parents':[...]。
import json
txt = '''{"efficiency_d": "T", "efficiency_c": null, "efficiency_a": null, "color": "Ryz.", "descendants_top_efficiency": {"a": null, "s": null, "c": null, "d": null}, "descendants": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "Ryz.", "internal_number_uek": null, "name_uek": "Nordico D' Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "cd93f4a4-e48b-4bcc-a8bb-a9901f11f2cf", "birth_year": 2017}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "B\u011bl.", "internal_number_uek": null, "name_uek": "Lidador d'Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "b1b24446-7d6d-486c-ab46-bff6d77ed1dc", "birth_year": 2015}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "B\u011bl.", "internal_number_uek": null, "name_uek": "Jade Dos Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "a1d1ff36-8666-4fbf-8cd6-79eb427eb2c7", "birth_year": 2014}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": "Ryz.", "internal_number_uek": null, "name_uek": "Jandaia D'Pardinhos", "race": "LUSITANO", "efficiency_s": null, "licence": null, "id": "4c19ebb2-6270-4a24-b322-ec035ae136cd", "birth_year": 2014}], "internal_number_uek": null, "name_uek": "Destino Pardinhos", "race": "LUSITANO", "parents": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Tixaquir", "race": "LUSITANO", "parents": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Xaquiro", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Oxigenada", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}], "efficiency_s": null, "licence": null, "id": "f62937b0-1af5-49aa-b65e-ec1b2849e529", "birth_year": 2000}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Unanime", "race": "LUSITANO", "parents": [{"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Navegante", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}, {"efficiency_d": null, "efficiency_c": null, "efficiency_a": null, "color": null, "internal_number_uek": null, "name_uek": "Piza", "race": null, "parents": [{"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}, {"parents": [{"parents": [{}, {}]}, {"parents": [{}, {}]}]}], "efficiency_s": null, "licence": null, "id": null, "birth_year": null}], "efficiency_s": null, "licence": null, "id": "b13858af-2b60-4319-bd38-928912d6871a", "birth_year": 2001}], "efficiency_s": null, "licence": "KJ00TU", "id": "0c827a0a-9489-4580-a373-d20c5e27859f", "birth_year": 2008}'''
data = json.loads(txt)
def traverse(d):
if 'parents' in d:
sire, dam = d['parents']
del d['parents']
if sire:
d['sire'] = sire
traverse(sire)
if dam:
d['dam'] = dam
traverse(dam)
traverse(data)
print(json.dumps(data, indent=4))打印:
{
"efficiency_d": "T",
"efficiency_c": null,
"efficiency_a": null,
"color": "Ryz.",
"descendants_top_efficiency": {
"a": null,
"s": null,
"c": null,
"d": null
},
"descendants": [
{
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": "Ryz.",
"internal_number_uek": null,
"name_uek": "Nordico D' Pardinhos",
"race": "LUSITANO",
"efficiency_s": null,
"licence": null,
"id": "cd93f4a4-e48b-4bcc-a8bb-a9901f11f2cf",
"birth_year": 2017
},
{
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": "B\u011bl.",
"internal_number_uek": null,
"name_uek": "Lidador d'Pardinhos",
"race": "LUSITANO",
"efficiency_s": null,
"licence": null,
"id": "b1b24446-7d6d-486c-ab46-bff6d77ed1dc",
"birth_year": 2015
},
{
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": "B\u011bl.",
"internal_number_uek": null,
"name_uek": "Jade Dos Pardinhos",
"race": "LUSITANO",
"efficiency_s": null,
"licence": null,
"id": "a1d1ff36-8666-4fbf-8cd6-79eb427eb2c7",
"birth_year": 2014
},
{
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": "Ryz.",
"internal_number_uek": null,
"name_uek": "Jandaia D'Pardinhos",
"race": "LUSITANO",
"efficiency_s": null,
"licence": null,
"id": "4c19ebb2-6270-4a24-b322-ec035ae136cd",
"birth_year": 2014
}
],
"internal_number_uek": null,
"name_uek": "Destino Pardinhos",
"race": "LUSITANO",
"efficiency_s": null,
"licence": "KJ00TU",
"id": "0c827a0a-9489-4580-a373-d20c5e27859f",
"birth_year": 2008,
"sire": {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Tixaquir",
"race": "LUSITANO",
"efficiency_s": null,
"licence": null,
"id": "f62937b0-1af5-49aa-b65e-ec1b2849e529",
"birth_year": 2000,
"sire": {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Xaquiro",
"race": null,
"efficiency_s": null,
"licence": null,
"id": null,
"birth_year": null,
"sire": {
"sire": {},
"dam": {}
},
"dam": {
"sire": {},
"dam": {}
}
},
"dam": {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Oxigenada",
"race": null,
"efficiency_s": null,
"licence": null,
"id": null,
"birth_year": null,
"sire": {
"sire": {},
"dam": {}
},
"dam": {
"sire": {},
"dam": {}
}
}
},
"dam": {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Unanime",
"race": "LUSITANO",
"efficiency_s": null,
"licence": null,
"id": "b13858af-2b60-4319-bd38-928912d6871a",
"birth_year": 2001,
"sire": {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Navegante",
"race": null,
"efficiency_s": null,
"licence": null,
"id": null,
"birth_year": null,
"sire": {
"sire": {},
"dam": {}
},
"dam": {
"sire": {},
"dam": {}
}
},
"dam": {
"efficiency_d": null,
"efficiency_c": null,
"efficiency_a": null,
"color": null,
"internal_number_uek": null,
"name_uek": "Piza",
"race": null,
"efficiency_s": null,
"licence": null,
"id": null,
"birth_year": null,
"sire": {
"sire": {},
"dam": {}
},
"dam": {
"sire": {},
"dam": {}
}
}
}
}https://stackoverflow.com/questions/62504271
复制相似问题