response = {'links': [{'rel': 'self', 'href': 'XXXXXXX'}], 'id': 22, 'attribute_type': 'Number', 'label': 'Type', 'required': True, 'constrained': True, 'order': 2, 'allowed_values': [{'links': [], 'value': 701, 'label': 'Manual', 'order': 1, 'is_default': True, 'is_active': True}, {'links': [], 'value': 702, 'label': 'Automation', 'order': 2, 'is_default': False, 'is_active': True}, {'links': [], 'value': 703, 'label': 'Performance', 'order': 3, 'is_default': False, 'is_active': True}, {'links': [], 'value': 704, 'label': 'Scenario', 'order': 4, 'is_default': False, 'is_active': True}], 'multiple': False, 'data_type': 3, 'searchable': True, 'free_text_search': False, 'search_key': 'type', 'system_field': True, 'original_name': 'Type', 'is_active': True}
所以我可以这样在响应中获取ID:
for r in response:
if r['label'] == 'Type'
return r['id']
但是,我试图在响应的“ allowed_values”列表中获取“ value”。
'allowed_values': [{'links': [], 'value': 701, 'label': 'Manual', 'order': 1, 'is_default': True, 'is_active': True}, {'links': [], 'value': 702, 'label': 'Automation', 'order': 2, 'is_default': False, 'is_active': True}, {'links': [], 'value': 703, 'label': 'Performance', 'order': 3, 'is_default': False, 'is_active': True}, {'links': [], 'value': 704, 'label': 'Scenario', 'order': 4, 'is_default': False, 'is_active': True}]
有可能吗?
感谢您的帮助。预先感谢!
在安东的帮助下,这是我最终使用的!
if r.get('allowed_values'):
av = r.get('allowed_values')
for av_values in av:
if av_values['label'] == subvalue:
field_value = av_values['value']
return field_value
答案 0 :(得分:1)
这是列表推导在Python中派上用场的地方。嗯,虽然很基本。我会看看我能不能找到骗子。
values = [i['value'] for i in response['allowed_values']]
print(values) # --> prints[701, 702, 703, 704]