问题描述
对接第三方接口拿到的数据如下图,将data中的数据转为字符串后保存数据库,但是会报错。
{
"code": "0",
"data": {
"callSign": "13001",
"courseOverGround": null,
"destination": "\\'I2\\'I2\\'I2\\'I2\\'I2",
"draught": 11.4,
"effectiveFlag": false,
"eta": "2021-02-14 09:50:00",
"hasSourceMmsi": true,
"id": "e0f01feae67c40b0aa0506458d301e451598490252736",
"imoCode": 460008,
"isWarning": false,
"latitude": null,
"length": 24,
"longitude": null,
"navigationStatus": null,
"otherInfo": null,
"picList": null,
"rateOfTurn": null,
"shipName": "SUDINGTAIYU11888",
"shipType": 90,
"shipTypeStr": "其他船只",
"sourceMmsi": 412364876,
"speedOverGround": null,
"trueHeading": null,
"width": 5,
"windNumber": null
},
"message": "操作成功"
}
报错原因
数据库不能直接保存值 “\'I2\'I2\'I2\'I2\'I2” 原因是里面含有特殊字符。
解决
办法(并不能一劳永逸解决所有类似的问题)
如果能成功存入数据库,就必须对字符串改造,替换字符串中的特殊字符
改造后示例1:
去掉一个\,字符串前面加E
改造后示例2:
去掉单引号’