MySQL更新存放JSON的字段、\\“ 转义成 “的问题描述
作者:七海健人 时间:2024-01-13 16:30:00
本篇介绍在执行MySQL线上变更时遇到的问题,表现为"更新JSON字段时,实际更新的值与SQL语句中的值不一致,JSON格式错误",如下;
问题描述
处理线上问题,需要新插入一条记录;将原JSON粘贴出来,修改部分字段,然后提交SQL更新语句,原始JSON如下:
{
"playerQualifyType": 1,
"surveyId": 14,
"playerRegisterEndTime": 1670860799000,
"planName": "《碧蓝大陆》先锋测试招募!",
"planStatus": 2,
"playerRegisterStartTime": 1669860000000,
"gameName": "碧蓝大陆",
"developerName": "海南羽弘信息技术有限公司",
"planStartTime": 1673316000000,
"qualificationCodeUrl": "",
"updateTime": 1669777099000,
"planPlayerNum": 1500,
"extend": "{\"innerDesc\":\"\",\"planId\":16,\"verifyOperator\":\"Akira-11126666\",\"verifyResultDesc\":\"\",\"verifyResultDescImgList\":[],\"verifyResultStatus\":2,\"verifyTime\":1669777099887}",
"planEndTime": 1673884800000,
"pkgStatus": 0
}
现提交SQL变更单,执行SQL语句如下:
update t set `json_field` =
'
{
"playerQualifyType": 1,
"surveyId": 14,
"playerRegisterEndTime": 1670860799000,
"planName": "《碧蓝大陆》先锋测试招募!",
"planStatus": 2,
"playerRegisterStartTime": 1669860000000,
"gameName": "碧蓝大陆",
"developerName": "海南羽弘信息技术有限公司",
"planStartTime": 1673316000000,
"qualificationCodeUrl": "",
"updateTime": 1669777099000,
"planPlayerNum": 1500,
"extend": "{\"innerDesc\":\"\",\"planId\":16,\"verifyOperator\":\"Akira-11126666\",\"verifyResultDesc\":\"\",\"verifyResultDescImgList\":[],\"verifyResultStatus\":2,\"verifyTime\":1669777099887}",
"planEndTime": 1673884800000,
"pkgStatus": 0
}
'
where id = 55;
结果数据库中更新后查出来的纪录如下,不满足JSON格式了,仔细检查发现是extend字段的值中的反斜杠全部没有了,JSON解析失败;
{
"playerQualifyType": 1,
"surveyId": 14,
"playerRegisterEndTime": 1670860799000,
"planName": "《碧蓝大陆》先锋测试招募!",
"planStatus": 2,
"playerRegisterStartTime": 1669860000000,
"gameName": "碧蓝大陆",
"developerName": "海南羽弘信息技术有限公司",
"planStartTime": 1673316000000,
"qualificationCodeUrl": "",
"updateTime": 1669777099000,
"planPlayerNum": 1500,
"extend": "{"innerDesc":"","planId":16,"verifyOperator":"Akira-11126666","verifyResultDesc":"","verifyResultDescImgList":[],"verifyResultStatus":2,"verifyTime":1669777099887}",
"planEndTime": 1673884800000,
"pkgStatus": 0
}
问题排查
联系DBA描述问题,得到的回复是——
\"相当于就转义成 "了,这是MySQL自身处理的结果;
实验如下:
而代码中执行的就没问题,原因是代码中该字段对应的Java类型为String,也就是说原JSON的外层因为套了一层双引号"",因此还会有一次转义符的嵌套,如下;
\"extend\":\"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\"
因此,代码中执行JSON字符串的MySQL插入,实际入库的值与预期一致;正确的SQL如下:
update t set `json_field` =
'
{\"playerQualifyType\":1,\"surveyId\":14,\"playerRegisterEndTime\":1670860799000,\"planName\":\"《碧蓝大陆》先锋测试招募!\",\"planStatus\":2,\"playerRegisterStartTime\":1669860000000,\"gameName\":\"碧蓝大陆\",\"developerName\":\"海南羽弘信息技术有限公司\",\"planStartTime\":1673316000000,\"qualificationCodeUrl\":\"\",\"updateTime\":1669777099000,\"planPlayerNum\":1500,\"extend\":\"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\",\"planEndTime\":1673884800000,\"pkgStatus\":0}
'
where id = 55;
解决方案
在更新Mysql表的JSON字符串字段时,步骤应该如下:
1. 输入查询语句,找到目标记录,复制JSON字段的值到JSON编辑工具中修改JSON中的属性值
2. 将修改后的JSON粘贴到IDE中,IDE用双引号接收这一串JSON;
3. 执行SQL时,SQL语句中的值以IDE中被双引号包裹的字符串值为SQL中的新的value;
// 将JSON粘贴进来 整体作为SQL更新语句中的新的value 这里也可以不格式化JSON
String formatValue = "{\n" +
" \"playerQualifyType\": 1,\n" +
" \"surveyId\": 14,\n" +
" \"playerRegisterEndTime\": 1670860799000,\n" +
" \"planName\": \"《碧蓝大陆》先锋测试招募!\",\n" +
" \"planStatus\": 2,\n" +
" \"playerRegisterStartTime\": 1669860000000,\n" +
" \"gameName\": \"碧蓝大陆\",\n" +
" \"developerName\": \"海南羽弘信息技术有限公司\",\n" +
" \"planStartTime\": 1673316000000,\n" +
" \"qualificationCodeUrl\": \"\",\n" +
" \"updateTime\": 1669777099000,\n" +
" \"planPlayerNum\": 1500,\n" +
" \"extend\": \"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\",\n" +
" \"planEndTime\": 1673884800000,\n" +
" \"pkgStatus\": 0\n" +
"}";
// 未格式化 会少去/n换行符
String value = "{\"playerQualifyType\":1,\"surveyId\":14,\"playerRegisterEndTime\":1670860799000,\"planName\":\"《碧蓝大陆》先锋测试招募!\",\"planStatus\":2,\"playerRegisterStartTime\":1669860000000,\"gameName\":\"碧蓝大陆\",\"developerName\":\"海南羽弘信息技术有限公司\",\"planStartTime\":1673316000000,\"qualificationCodeUrl\":\"\",\"updateTime\":1669777099000,\"planPlayerNum\":1500,\"extend\":\"{\\\"innerDesc\\\":\\\"\\\",\\\"planId\\\":16,\\\"verifyOperator\\\":\\\"Akira-11126666\\\",\\\"verifyResultDesc\\\":\\\"\\\",\\\"verifyResultDescImgList\\\":[],\\\"verifyResultStatus\\\":2,\\\"verifyTime\\\":1669777099887}\",\"planEndTime\":1673884800000,\"pkgStatus\":0}";
来源:https://blog.csdn.net/minghao0508/article/details/128372093
标签:MySQL,JSON,转义
0
投稿
猜你喜欢
MySQL数据库配置优化的方案
2024-01-14 00:41:52
Go语言题解LeetCode1266访问所有点的最小时间示例
2023-08-29 08:10:39
如何在sae中设置django,让sae的工作环境跟本地python环境一致
2022-03-09 22:04:54
.Net Core服务治理Consul搭建集群
2024-05-09 09:03:07
Python读取excel文件中的数据,绘制折线图及散点图
2023-09-12 11:47:31
Python OpenCV视频截取并保存实现代码
2023-01-05 06:04:21
JavaScript游戏开发之键盘控制层的移动
2008-09-13 19:29:00
Python Flask框架实现Proteus仿真Arduino与网页数据交互
2022-01-18 08:43:16
Python堆排序原理与实现方法详解
2021-02-22 15:07:31
在Django中实现添加user到group并查看
2021-12-08 21:47:07
Django websocket原理及功能实现代码
2023-04-18 16:49:11
豆瓣可以做而且值得做的几件事情
2009-04-24 12:07:00
asp随机生成文件名的函数
2009-02-11 13:41:00
ASP程序员面试题
2011-09-15 20:51:20
perl ping检测功能脚本代码
2023-09-27 12:41:47
Python的SQLalchemy模块连接与操作MySQL的基础示例
2024-01-20 07:37:13
Python3实现打格点算法的GPU加速实例详解
2022-05-26 23:20:30
js实现炫酷的左右轮播图
2024-04-22 22:31:40
js实现千分符和保留几位小数的简单实例
2023-08-21 10:03:26
一个简单的鼠标划过切换效果js源码
2010-06-21 10:55:00