首页 > 解决方案 > Python 从 mysql TypeError 中检索 JSON:JSON 对象必须是 str、bytes 或 bytearray,而不是 tuple

问题描述

当我尝试从中检索JSON时,MYSQL为什么会出现此错误?我该如何解决这个问题?

`def get_go(order_no):

mycursor.execute("SELECT `order_details` FROM `orders` WHERE `order_no` = " + str(order_no))

myresult = mycursor.fetchall()

for x in myresult:
    return json.loads(x)`

它返回此错误。

文件“/usr/lib/python3.7/json/init .py”,第 341 行,加载引发 TypeError( f'JSON对象必须是 str, bytes 或 bytearray, ' TypeError: JSON 对象必须是 str, bytes或字节数组,而不是元组

当我尝试使用

x = 'my_json_data_here'
y = json.loads(x)

它工作正常。为什么?

标签: mysqljsonpython-3.x

解决方案


我找到了原因。

为什么使用这种方法来检索JSON,数据带有一个('json_data',),它不是一个有效的json。我不知道为什么。但是我对从 mysql connection.cursor(dictionary=True) 检索数据进行了一些更改,并且它带有不带 (' ',) 的值


推荐阅读