您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql解析json数据组怎么获取数据组所有字段

2024/3/24 16:16:04发布23次查看
引言在开发过程中,遇到过json数据组的字符串,需要解析json组,得到组内所有的信息。如下格式:
[{"itemid":3101,"itemname":"空滤器及进气管道"},{"itemid":3102,"itemname":"水管、水泵"},{"itemid":3103,"itemname":"柴油管道"},{"itemid":3104,"itemname":"高压泵、机油泵"}]
观察json组发现,它是一个list里面包含多个json字符串,我们要做的是拆分出list所有json字符串,并对每个json字符串做解析。
分析后发现,当json字符串独立存在时,可以使用json_extract方法进行提取。由于列表中包含多个json字符串,因此我们需要将列表拆分为多个json字符串。
在学习本文内容之前,需要提前了解mysql两个函数:
substring_index
json_extract
第一步:一行拆分成多行一行拆成多行,即把list拆分成多行 json,为此我们需要
1.1 新建一张表keyid,只insert从0开始的数字,如下:
在其他的教程中,通过 mysql.help_topic 表的 help_topic_id 字段也是可以的。但是这个库表需要root权限才可以使用。因此建立自己的匹配表,是最合适的。
注意:id的值,不能小于 list里面json字符串的个数。举例来说,如果在上述列表中有4个json字符串,那么id值必须大于4。help_topic_id最大值是700,如果list里面json字符串的个数大于这个值,用help_topic_id是不合适的。
1.2 找到拆分标识符所谓拆分标识符,就是能根据此符号,一次性拆分成多行的标志。在下面list当中,没有找到拆分标识符,因此需要处理一下。可以将 ; 当成拆分标识符。处理后的内容如下:
{"itemid":3101,"itemname":"空滤器及进气管道"};{"itemid":3102,"itemname":"水管、水泵"};{"itemid":3103,"itemname":"柴油管道"};{"itemid":3104,"itemname":"高压泵、机油泵"}
去除前后 [ 和 ] 两个list标志,将 },{ 变成 };{ 这样就可以将 ; 变成拆分标识符。如下
select replace(replace(replace(jsonarr,"},{","};{"),"]",""),"[","") as jsonarr from maptest
1.3 通过join on拆分多行此时,我们可以通过使用join操作将maptest表和新建的keyid表连接起来,在on条件下匹配多行数据。在通过 substring_index进行拆分。
代码如下:
select a.jsonarr,substring_index( substring_index( a.jsonarr, ";", b.id + 1 ), ";",- 1 ) as jsonarr_info,b.idfrom (select replace(replace(replace(jsonarr,"},{","};{"),"]",""),"[","") as jsonarr from maptest) ajoin keyid b on b.id< ( length( a.jsonarr ) - length( replace ( a.jsonarr, ";", "" ) ) + 1 );
到此,就完成了 将json组,拆分成多行的工作。
第二步:解析json字符串拆分成多行之后,就可以通过 json_extract 进行解析了。效果如下:
完成代码如下:
select a.jsonarr,substring_index( substring_index( a.jsonarr, ";", b.id + 1 ), ";",- 1 ) as jsonarr_info,b.id,json_extract(substring_index( substring_index( a.jsonarr, ";", b.id + 1 ), ";",- 1 ), '$[0].itemid') as itemid,replace(json_extract(substring_index( substring_index( a.jsonarr, ";", b.id + 1 ), ";",- 1 ), '$[0].itemname'),'"','') as itemnamefrom (select replace(replace(replace(jsonarr,"},{","};{"),"]",""),"[","") as jsonarr from maptest) ajoin keyid b on b.id< ( length( a.jsonarr ) - length( replace ( a.jsonarr, ";", "" ) ) + 1 );
当然通过 mysql.help_topic 表的 help_topic_id 字段也是可以。代码和结果如下:
select a.jsonarr,substring_index( substring_index( a.jsonarr, ";", b.help_topic_id + 1 ), ";",- 1 ) as jsonarr_info,b.help_topic_id,json_extract(substring_index( substring_index( a.jsonarr, ";", b.help_topic_id + 1 ), ";",- 1 ), '$[0].itemid') as itemid,replace(json_extract(substring_index( substring_index( a.jsonarr, ";", b.help_topic_id + 1 ), ";",- 1 ), '$[0].itemname'),'"','') as itemnamefrom (select replace(replace(replace(jsonarr,"},{","};{"),"]",""),"[","") as jsonarr from maptest) ajoin mysql.help_topic b on b.help_topic_id < ( length( a.jsonarr ) - length( replace ( a.jsonarr, ";", "" ) ) + 1 );
注意: 通过 json_extract 解析出来的字段,如果是字符串,会带有 双引号,只要replace替换掉即可。
以上就是mysql解析json数据组怎么获取数据组所有字段的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product