从JSON输出中选择特定值

wgeznvg7  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(117)

我正在查询REST API,我需要从下面的适配器输出中选择2个字段。我基本上需要从OUT_Detailed Description和OUT_Vendor Ticket Number中创建变量:
代码:

headers = {'content-type': 'application/json', 'Authentication-Token': authToken}
response = requests.post('http://dev.startschools.local:2031/baocdp/rest/process/:ITSM_Interface:IncidentManagement:QueryIncident/execute', headers=headers, data=json.dumps(get_query_inc_json()))
 
print(response.text)
json_format = json.loads(response)
Description = (json_format['OUT_Detailed Decription'])
Ref_Number = (json_format['OUT_Vendor Ticket Number'])

response.text printed输出:

[{"name":"OUT_HPD_CI","value":"001"},{"name":"OUT_Detailed Description","value":"Student needs a new card issued"},{"name":"OUT_Vendor Ticket Number","value":"INC0000019"}]

错误:

in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not Response
PS C:\Program Files\SB_Python\AD Extract>

我尝试了几种方法来从输出中获取OUT_Detailed Description和OUT_Vendor Ticket Number值,但都失败了。

41ik7eoe

41ik7eoe1#

列表中有3个字典,首先获取列表中想要的字典,并获取“value”键的值。

Description = (json_format[1]['value')
Ref_Number = (json_format[2]['value'])

如果你想要更好的东西,用这个:

def restArrayToDict(restArray):
    dictResult = {}
    for dict in restArray:
        dictResult[dict['name']] = dict['value']
    return dictResult
dw1jzc5e

dw1jzc5e2#

你有没有试过做json_format = json.loads(response.content.decode('utf-8'))来将你的响应转换成字符串?

相关问题