im我正在尝试解析json数据,并在每次元素时收到此错误 if ['fields']['assignee'] in each: TypeError: list indices must be integers or slices, not str >>> 我的 json 是这个{"expand": "schema,names","startAt": 1,"maxResults": 50,"total": 7363,"issues": [ { "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields", "id": "591838", "self": "https://jira.mynet.com/rest/api/2/issue/591838", "key": "TEST-8564", "fields": { "summary": "delete tables 31-03-2020 ", "customfield_10006": 2.0, "created": "2020-02-27T10:29:12.000+0100", "description": "A LOT OF TEXT", "assignee": null, "labels": [ "DATA", "Refined" ], "status": { "self": "https://jira.mynet.com/rest/api/2/status/10000", "description": "", "iconUrl": "https://jira.mynet.com/", "name": "To Do", "id": "10000", "statusCategory": { "self": "https://jira.mynet.com/rest/api/2/statuscategory/2", "id": 2, "key": "new", "colorName": "blue-gray", "name": "To Do" } } } } ]}在此示例中,['fields']['assignee'] 中的元素为 NULL,有时如下所示我正在尝试检查受让人是否为空,如果是这样,请打印空值
1 回答
浮云间
TA贡献1829条经验 获得超4个赞
尝试
if 'fields' in each and 'assignee' in each['fields']:
请注意,您需要密钥的名称,而不是用方括号括起来。
也许更好:
for each in data['issues']: print(each.get('fields', {}).get('assignee', {}).get('name', 'null'))
如果您不能保证“问题”存在于以下两个方面:data
for each in data.get('issues', []): <as before>
data.get('issues', [])
如果不存在,则返回空列表。data['issuess']
添加回答
举报
0/150
提交
取消