类型错误“bool”对象不是subscriptab

2024-06-16 09:57:08 发布

您现在位置:Python中文网/ 问答频道 /正文

但是,从我的JSON代码中获取JSON数据有点麻烦。在

url = 'https://api.test.net/Vacancy'
payload = {
    "APIKey": "0000",
    "Action": "GetAllVacancies",
    "Content-Type" : "json",
}

headers = {}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print(r.content)

cursor = mydb.cursor()

json_obj = r.json()
for index in json_obj:
            cursor.execute("INSERT INTO apidata (VacancyName, Department, Location) VALUES (%s, %s, %s)", (json_obj[index]["VacancyName"], (json_obj[index]["Department"], (json_obj[index]["Location"]))

cursor.close()

我的JSON响应如下所示

^{pr2}$

但是我一直得到错误

TypeError 'bool' object is not subscriptable

我正在尝试提取这个JSON数据并将其发送到数据库,谢谢!在


Tags: 数据代码httpsapijsonobjurlindex
2条回答

您的JSON对象是dict。迭代dict遍历键。在

对象中的第一个键是"isError",因此当您试图访问json_obj[index]["Department"]时,它相当于(json_obj["isError"]["Department"],它是{},它给出了您看到的错误。在

以后请包括完整的错误信息,包括回溯。如果你这样做,回答这些问题会容易得多。在

看看

for index in json_obj:
    cursor.execute("INSERT INTO apidata (VacancyName, Department, Location) VALUES (%s, %s, %s)", (json_obj[index]["VacancyName"], (json_obj[index]["Department"], (json_obj[index]["Location"]))

循环json_obj中的键,第一个键是isError。所以json_obj[index]是{},因此不可下标,当您试图访问key[“VacancyName”]时会出错。在

您需要循环json_obj["Result"]中的元素

^{pr2}$

相关问题 更多 >