mysql - 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)
它工作正常。为什么?
解决方案
我找到了原因。
为什么使用这种方法来检索JSON,数据带有一个('json_data',),它不是一个有效的json。我不知道为什么。但是我对从 mysql connection.cursor(dictionary=True) 检索数据进行了一些更改,并且它带有不带 (' ',) 的值
推荐阅读
- ruby-on-rails - 覆盖/自定义 ActiveRecord::Associations::CollectionProxy 方法构建和引用关联实例
- python - 我发送消息后,Discord.py bot 不断在 dm 上发送垃圾邮件
- jquery - 如何在 jquery 中切换颜色属性?
- android - Android 中 Fragments 中的 ViewBindng 问题
- email - MUTT - 如何在 IMAP“[Gmail]/Sent Mail”文件夹中保存发送的消息(通过不同的 SMTP 服务器)
- firebase - 仅在注册时使用 Google Firebase 和 Flutter 公司电子邮件
- java - 有没有一种简单的方法可以在 android 中获得整数天数?
- sql - 为逻辑句动态生成 SQL
- python - 谷歌 Colab 段落删除
- javascript - 访问 RTCMediaStreamTrack 统计信息,注入导致问题