<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本篇介紹在執行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": 0n" + "}"; // 未格式化 會少去/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}";
到此這篇關於MySQL更新存放JSON的欄位、“ 跳脫成 “的文章就介紹到這了,更多相關MySQL JSON跳脫內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45