gpt4 book ai didi

python - JSON 请求出现类型错误

转载 作者:行者123 更新时间:2023-12-01 06:55:49 26 4
gpt4 key购买 nike

import requests

user = 'my_user'
pwd = 'my_pwd'
headers = {"Content-Type":"application/json","Accept":"application/json"}

url = 'https://my_company.service-now.com/api/now/table/sc_req_item/bfa27ef2db203f00d8909c47db9619f3?sysparm_fields=sys_id%2Crequest'

response = requests.get(url, auth=(user, pwd), headers=headers )
mydata = response.json()
print(mydata) #Print for debug output purposes
for item in mydata["result"]:
print(item["sys_id"])
print(item["request"]["value"])

来自print(mydata)的输出:

{'result': {'sys_id': 'bfa27ef2db203f00d8909c47db9619f3', 'request': {'link': 'https://my_company.service-now.com/api/now/table/sc_request/3fa27ef2db203f00d8909c47db9619f3', 'value': '3fa27ef2db203f00d8909c47db9619f3'}}}

错误:

Traceback (most recent call last):
File "./prod_getUnapprovedSIDs.py", line 42, in <module>
print(item["sys_id"])
TypeError: string indices must be integers

最佳答案

您需要迭代 JSON 响应的键、值对。试试这个例子:

for key, item in mydata.items():
print(item["sys_id"])
print(item["request"]["value"])

关于python - JSON 请求出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com