小编Ste*_*fan的帖子

如何将 JSON MySQL 列查询到字典中?

我的数据库如下所示:

CREATE TABLE my_table( irrelevant_column TEXT, json_stuff JSON );
Run Code Online (Sandbox Code Playgroud)
mysql> SELECT * FROM my_table;

+------------------------+-----------------------------------------------+
| irrelevant_column      | json_stuff                                    |
+------------------------+-----------------------------------------------+
| blah blah              | { 'this': 'is my json data' }                 |
| more unrelated stuff   | { 'more': 'of my data in the correct format' }|
+------------------------+-----------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种优雅的方法来将 MySQL 中的 JSON 数据查询到字典列表中。

我尝试过使用DictCursor

cursor = connection.cursor( pymysql.cursors.DictCursor )
cursor.execute( "SELECT json_stuff FROM my_table" )
rows = cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)

但它不能正确处理 JSON 列类型。它返回这个:

[
  { "json_stuff": "<json_stuff row 1 in …
Run Code Online (Sandbox Code Playgroud)

python json pymysql

5
推荐指数
1
解决办法
2863
查看次数

标签 统计

json ×1

pymysql ×1

python ×1