【问题标题】:Python retrieve JSON from mysql TypeError: the JSON object must be str, bytes or bytearray, not tuplePython 从 mysql TypeError 中检索 JSON:JSON 对象必须是 str、bytes 或 bytearray,而不是 tuple
【发布时间】:2021-04-19 06:58:28
【问题描述】:

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

`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 行,加载中 raise TypeError(f'JSON对象必须是str, bytes or bytearray, ' TypeError: JSON对象必须是str, bytes or bytearray, 不是 元组

当我尝试使用

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

它工作正常。为什么?

【问题讨论】:

  • 你如何获得标记为my_json_data_here的部分?

标签: mysql json python-3.x


【解决方案1】:

我找到了原因。

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

【讨论】:

  • 我遇到了同样的错误。我正在尝试使用您在上面提供的解决方案来解决此问题,但它不起作用。
猜你喜欢
  • 2017-07-10
  • 2019-08-27
  • 2021-06-17
  • 2022-12-08
  • 2022-11-24
  • 1970-01-01
  • 2021-09-09
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多