如何使用python循环字典类型数据列表的字典

问题描述 投票:0回答:1

我正在尝试使用 python 循环

{u'customfield_10067': [u'{"field_type":"cmdb-object-cftype","value":{"workspaceId":"dsjahdjasf-61f7-4421-9ced-df122a122d603","name":"test","objectId":"14","id":"dsjahdjasf-61f7-4421-9ced-df122a122d603:14"}}']}

获取字典中自定义字段的值

{
customfield_10067 : 
{"workspaceId":"dsjahdjasf-61f7-4421-9ced-df122a122d603","objectId":"14","id":"dsjahdjasf-61f7-4421-9ced-df122a122d603:14"}
}

我尝试循环作为字典和字典迭代的列表,但是当我作为字典循环时它会抛出错误

TypeError: ValueError('dictionary update sequence element #0 has length 1; 2 is required',) is not JSON serializable

作为字典列表循环时 它会抛出字符串索引错误

非常感谢任何帮助。

custom_fields = [] regex = '[^A-Za-z0-9]+' project_id = request.data['project_id'] description = re.sub(regex, ' ', str(request.data['description'])) summary = re.sub(regex, ' ', str(request.data['subject'])) issue_type_id = re.sub(regex, '', str(request.data['issuetype'])) request_dict = dict(request.data)

for fields,value in request_dict.items(): if "custom" in fields: custom_data = {fields : value} custom_fields.append(custom_data) # print(custom_fields) for k,v in dict(custom_fields).items(): print(k) print(v)
python list dictionary iteration jira
1个回答
0
投票

也许

eval()
能够完成工作,尽管可能需要更多验证,具体取决于您的列表包含多少个字符串,但您明白了。

dict_list = {u'customfield_10067': [u'{"field_type":"cmdb-object-cftype","value":{"workspaceId":"dsjahdjasf-61f7-4421-9ced-df122a122d603","name":"test","objectId":"14","id":"dsjahdjasf-61f7-4421-9ced-df122a122d603:14"}}']}
new_dict = {}
for key, value in dict_list.items():
    new_dict[key] = eval(value[0])['value']
print(new_dict)

输出:

{'customfield_10067': {'workspaceId': 'dsjahdjasf-61f7-4421-9ced-df122a122d603', 'name': 'test', 'objectId': '14', 'id': 'dsjahdjasf-61f7-4421-9ced-df122a122d603:14'}}
© www.soinside.com 2019 - 2024. All rights reserved.