mysqljson_extract用法,【MySQL】json_extract解析json

更新时间:2023-07-23 13:58:22 阅读: 评论:0

mysqljson_extract⽤法,【MySQL】json_extract解析json MySQL5.7
json串如下:
{"Data":{"List":[{"ID":"101010","NAME":"ZHANGSAN","IDCARD":"110101000001291"}, {"ID":"201010","NAME":"LISI","IDCARD":"220101000001291"}]}}
送给孩子的一首歌json_extract解析json
mysql> SET @json = '{"Data":{"List":[{"ID":"101010","NAME":"ZHANGSAN","IDCARD":"110101000001291"}, {"ID":"201010","NAME":"LISI","IDCARD":"220101000001291"}]}}';
Query OK, 0 rows affected
mysql>
mysql> SELECT json_extract(@json,'$.Data.List[0].ID') as id1,
json_extract(@json,'$.Data.List[0].NAME') as name1,
json_extract(@json,'$.Data.List[1].ID') as id2,
json_extract(@json,'$.Data.List[1].NAME') as name2;
+----------+------------+----------+--------+
win10关机| id1 | name1 | id2 | name2 |
+----------+------------+----------+--------+
| "101010" | "ZHANGSAN" | "201010" | "LISI" |
+----------+------------+----------+--------+一碗白米饭
1 row in t
mysql>
问题:解析后的字符串都包含双引号,解决此问题有如下两个⽅法
⽅法⼀:json_unquote()
mysql> SET @json = '{"Data":{"List":[{"ID":"101010","NAME":"ZHANGSAN","IDCARD":"110101000001291"}, {"ID":"201010","NAME":"LISI","IDCARD":"220101000001291"}]}}';
口蘑的营养价值与功效SELECT json_extract(@json,'$.Data.List[0].ID') as strid,json_unquote(json_extract(@json,'$.Data.List[0].ID')) as intid;
Query OK, 0 rows affected
+----------+--------+
| strid | intid |冰糖泡
秋天的植物+----------+--------+
| "101010" | 101010 |
+----------+--------+
1 row in t
mysql>
⽅法⼆:->>
mysql> SELECT json_extract(myjson,'$.Data.List[0].ID') AS strid,myjson->>'$.Data.List[0].ID' as intid FROM
(SELECT '{"Data":{"List":[{"ID":"101010","NAME":"ZHANGSAN","IDCARD":"110101000001291"}, {"ID":"201010","NAME":"LISI","IDCARD":"220101000001291"}]}}' as myjson) t;
+----------+--------+
学包饺子作文| strid | intid |
+----------+--------+
| "101010" | 101010 |
+----------+--------+
1 row in t
收件箱
mysql>

本文发布于:2023-07-23 13:58:22,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1093346.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:双引号   问题   字符串   饺子   送给   口蘑   学包
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图