Python 如何在JSON中读取没有数字索引的字段
我有一个json文件,我需要以结构化的方式读取它,以便在数据库中在其各自的列中插入每个值,但在标记Python 如何在JSON中读取没有数字索引的字段,python,json,Python,Json,我有一个json文件,我需要以结构化的方式读取它,以便在数据库中在其各自的列中插入每个值,但在标记“customFields”中,字段更改索引,例如:“Tribe/Customer”可以是json块中的索引0(行['customFields'][0]),而在另一个块中是索引3(行)['customFields'][3]),因此我尝试使用行字段的名称读取数据['customFields']['Tribe/Customer'],但出现以下错误: TypeError:列表索引必须是整数或片,而不是st
“customFields”
中,字段更改索引,例如:“Tribe/Customer”
可以是json块中的索引0(行['customFields'][0])
,而在另一个块中是索引3(行)['customFields'][3])
,因此我尝试使用行字段的名称读取数据['customFields']['Tribe/Customer']
,但出现以下错误:
TypeError:列表索引必须是整数或片,而不是str
脚本:
def getCustomField(ModelData):
for row in ModelData["data"]["squads"][0]["cards"]:
print(row['identifier'],
row['customFields']['Tribe / Customer'],
row['customFields']['Stopped with'],
row['customFields']['Sub-Activity'],
row['customFields']['Activity'],
row['customFields']['Complexity'],
row['customFields']['Effort'])
if __name__ == "__main__":
f = open('test.json')
json_file = json.load(f)
getCustomField(json_file)
{
"data": {
"squads": [
{
"name": "TESTE",
"cards": [
{
"identifier": "0102",
"title": "TESTE",
"description": " TESTE ",
"status": "on_track",
"priority": null,
"assignees": [
{
"fullname": "TESTE",
"email": "TESTE"
}
],
"createdAt": "2020-04-16T15:00:31-03:00",
"secondaryLabel": null,
"primaryLabels": [
"TESTE",
"TESTE"
],
"swimlane": "TESTE",
"workstate": "Active",
"customFields": [
{
"name": "Tribe / Customer",
"value": "TESTE 1"
},
{
"name": "Checkpoint",
"value": "GNN"
},
{
"name": "Stopped with",
"value": null
},
{
"name": "Sub-Activity",
"value": "DEPLOY"
},
{
"name": "Activity",
"value": "TOOL"
},
{
"name": "Complexity",
"value": "HIGH"
},
{
"name": "Effort",
"value": "20"
}
]
},
{
"identifier": "0103",
"title": "TESTE",
"description": " TESTE ",
"status": "on_track",
"priority": null,
"assignees": [
{
"fullname": "TESTE",
"email": "TESTE"
}
],
"createdAt": "2020-04-16T15:00:31-03:00",
"secondaryLabel": null,
"primaryLabels": [
"TESTE",
"TESTE"
],
"swimlane": "TESTE",
"workstate": "Active",
"customFields": [
{
"name": "Tribe / Customer",
"value": "TESTE 1"
},
{
"name": "Stopped with",
"value": null
},
{
"name": "Checkpoint",
"value": "GNN"
},
{
"name": "Sub-Activity",
"value": "DEPLOY"
},
{
"name": "Activity",
"value": "TOOL"
},
{
"name": "Complexity",
"value": "HIGH"
},
{
"name": "Effort",
"value": "20"
}
]
}
]
}
]
}
}
JSON:
def getCustomField(ModelData):
for row in ModelData["data"]["squads"][0]["cards"]:
print(row['identifier'],
row['customFields']['Tribe / Customer'],
row['customFields']['Stopped with'],
row['customFields']['Sub-Activity'],
row['customFields']['Activity'],
row['customFields']['Complexity'],
row['customFields']['Effort'])
if __name__ == "__main__":
f = open('test.json')
json_file = json.load(f)
getCustomField(json_file)
{
"data": {
"squads": [
{
"name": "TESTE",
"cards": [
{
"identifier": "0102",
"title": "TESTE",
"description": " TESTE ",
"status": "on_track",
"priority": null,
"assignees": [
{
"fullname": "TESTE",
"email": "TESTE"
}
],
"createdAt": "2020-04-16T15:00:31-03:00",
"secondaryLabel": null,
"primaryLabels": [
"TESTE",
"TESTE"
],
"swimlane": "TESTE",
"workstate": "Active",
"customFields": [
{
"name": "Tribe / Customer",
"value": "TESTE 1"
},
{
"name": "Checkpoint",
"value": "GNN"
},
{
"name": "Stopped with",
"value": null
},
{
"name": "Sub-Activity",
"value": "DEPLOY"
},
{
"name": "Activity",
"value": "TOOL"
},
{
"name": "Complexity",
"value": "HIGH"
},
{
"name": "Effort",
"value": "20"
}
]
},
{
"identifier": "0103",
"title": "TESTE",
"description": " TESTE ",
"status": "on_track",
"priority": null,
"assignees": [
{
"fullname": "TESTE",
"email": "TESTE"
}
],
"createdAt": "2020-04-16T15:00:31-03:00",
"secondaryLabel": null,
"primaryLabels": [
"TESTE",
"TESTE"
],
"swimlane": "TESTE",
"workstate": "Active",
"customFields": [
{
"name": "Tribe / Customer",
"value": "TESTE 1"
},
{
"name": "Stopped with",
"value": null
},
{
"name": "Checkpoint",
"value": "GNN"
},
{
"name": "Sub-Activity",
"value": "DEPLOY"
},
{
"name": "Activity",
"value": "TOOL"
},
{
"name": "Complexity",
"value": "HIGH"
},
{
"name": "Effort",
"value": "20"
}
]
}
]
}
]
}
}
您必须将自定义字段列表解析为可以通过名称访问的内容。因为您要访问同一列表中的多个条目,所以字典是最合适的选择
for row in ModelData["data"]["squads"][0]["cards"]:
custom_fields_dict = {field['name']: field['value'] for field in row['customFields']}
print(row['identifier'],
custom_fields_dict['Tribe / Customer'],
...
)
如果只需要一个字段,则可以遍历列表以查找匹配项,但重复这样做效率较低
我跳过了处理缺失字段的步骤-如果json列表中可能不存在字段,您可能希望使用
get('Tribe/Customer',some\u reasonal\u default)
。这是因为您的一些元素在列表中。[“这里的任何内容都将是列表”]
要访问此列表,只需执行索引0或循环。可以显示什么是理想的解决方案吗?我只想确保json和银行的字段将收到正确的值'Tribe/Customer'
在两个块的0
索引中都存在。是的,但他并不总是以那种速率返回。我发现您的方法是cellent,非常感谢!我只是不理解使用一些合理的默认值的字段中缺少的部分如何在上面的示例中应用此项?假设for0103
没有收到`Tribe/Customer'
字段,如果您请求一个不存在的字段怎么办?对一个不存在的键进行字典解引用包含的ot将抛出一个KeyError
,get
方法返回None
,如果您没有指定默认值,或者您指定了值。例如custom\u fields\u dict.get('foobar','')
将返回(并打印)空字符串,或者可能您希望某个字段有其他标记,但该字段不存在。现在我明白了,我不知道这个get()
,非常感谢您解决了我的问题,并教了我一些新的东西!