发布网友 发布时间:2022-04-22 14:11
共3个回答
懂视网 时间:2022-05-01 21:54
创建表json_test:
CREATE TABLE json_test(id INT(11) AUTO_INCREMENT PRIMARY KEY,person_desc JSON)ENGINE INNODB;
插入一条记录:
INSERT INTO json_test(person_desc) VALUES (‘{ "programmers": [{ "firstName": "Brett", "lastName": "McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName": "Hunter", "email": "bbbb" }, { "firstName": "Elliotte", "lastName": "Harold", "email": "cccc" }], "authors": [{ "firstName": "Isaac", "lastName": "Asimov", "genre": "sciencefiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christianfiction" }], "musicians": [{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }] }‘);
查看插入的这行JSON数据有哪些KEY:
mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_testG *************************** 1. row *************************** id: 1 keys: ["authors", "musicians", "programmers"] 1 row in set (0.00 sec)
可以看到里面有三个KEY,分别为authors,musicians,programmers。那现在找一个KEY把对应的值拿出来:
mysql> SELECT json_extract(AUTHORS,‘$.lastName[0]‘) AS ‘name‘, AUTHORS FROM -> ( -> SELECT id,json_extract(person_desc,‘$.authors[0][0]‘) AS "authors" FROM json_test -> UNION ALL -> SELECT id,json_extract(person_desc,‘$.authors[1][0]‘) AS "authors" FROM json_test -> UNION ALL -> SELECT id,json_extract(person_desc,‘$.authors[2][0]‘) AS "authors" FROM json_test -> ) AS T1 -> ORDER BY NAME DESCG *************************** 1. row *************************** name: "Williams" AUTHORS: {"genre": "fantasy", "lastName": "Williams", "firstName": "Tad"} *************************** 2. row *************************** name: "Peretti" AUTHORS: {"genre": "christianfiction", "lastName": "Peretti", "firstName": "Frank"} *************************** 3. row *************************** name: "Asimov" AUTHORS: {"genre": "sciencefiction", "lastName": "Asimov", "firstName": "Isaac"} 3 rows in set (0.00 sec)
列出详细值:
mysql> SELECT -> json_extract(AUTHORS,‘$.firstName[0]‘) AS "firstname", -> json_extract(AUTHORS,‘$.lastName[0]‘) AS "lastname", -> json_extract(AUTHORS,‘$.genre[0]‘) AS "genre" -> FROM -> ( -> SELECT id,json_extract(person_desc,‘$.authors[0]‘) AS "authors" FROM json _test -> ) AS TG *************************** 1. row *************************** firstname: "Isaac" lastname: "Asimov" genre: "sciencefiction" 1 row in set (0.00 sec)
删掉authors这个KEY对应的所有对象:
mysql> UPDATE json_test SET person_desc = json_remove(person_desc,‘$.authors‘)G Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
查找对应的KEY,发现已经被删除掉:
mysql> SELECT json_contains_path(person_desc,‘all‘,‘$.authors‘) as authors_exist s FROM json_testG *************************** 1. row *************************** authors_exists: 0 1 row in set (0.00 sec)
MySQL5.7中新增的JSON类型的使用方法
标签:[1] ble esc inno hang warning author 创建 mat
热心网友 时间:2022-05-01 19:02
JSON 就是一种格式
不同类型的变量转换为JSON格式的方式稍有不同。
(1)将数值型、布尔型变量和函数对象转换为JSON格式非常简单,不需要做任何处理,
(2)将字符串型变量转换为JSON格式时,需要先处理其中的某些特殊字符。例如双引号、单引号和回车、换行符,将其替换为反斜杠"转义后
的字符,然后用引号将转换后的内容括起来。代码如下:
return(""""+obj.replace(/(["""'])/g,"""$1").replace(/"r/,"""r").replace(/"n/,"""n").replace(/"t/,"""t")+"""");
(3)将日期时间型变量转换为JSON格式时,需要将其进行特殊处理,代码如下:
return("(new Date("""+obj.toUTCString()+"""))");
(4)将正则表达式转换为JSON格式时,通过此对象的source获取其内容。不仅仅需要处理source中的特殊字符,还需要注意设置其
global,ignoreCase和multiline属性,代码如下:
return("(new RegExp("""+String(obj.source).replace(/("W)/g,"""$1")+""",
"""+(obj.global?"g":"")+(obj.ignoreCase?"i":"")+(obj.multiline?"m":"")+"""))");
(5)将数组对象转换为JSON格式时,递归调用toJSONString函数依次序列化其中的每个元素,然后将获得的各个字符串用逗号连接,在最外
围用方括号[]将其括起来,代码如下:
var re = new Array();
for(var i=0; i<obj.length; i++)re.push(toJSONString(obj[i]));
return("[" + re.join(", ") + "]");
(6)将Object对象转换为JSON格式时,做法与数组对象类似,同样需要通过递归调用来依次处理其中的每个组成元素。此外,需要注意的
是,其组成元素序列化时,需要构成名值对的形式。
热心网友 时间:2022-05-01 20:20
MySQL 从 5.7 开始,增加了新的数据类型:JSON。
JSON 类型的出现,弥补了 MySQL 长期以来相比其他关系型数据库的非标准化特性略势,比如可以用 JSON 来实现类似字典、数组等类型。以及之后大量针对 JSON 类型字串的相关遍历方法与函数。
比如对数组来说,简单的遍历路径为 "$[数组小标]" 或者 "$[*]" 来一次性遍历所有元素。MySQL 8.0 又新增了对于数组元素的范围遍历,比如 "$[m to n]",表示遍历下标为 m 到 n 的元素。