JSON.parse()——Uncaught SyntaxError: Unexpected token \ in JSON at position 1
背景:项目安全处理方面之一 ——对特殊字符进行编解码(后端编码,前端解码)
特殊字符:
" %22
\ %5C
/ %2F
& %26
% %25
' %27
; %3B
[ %5B
] %5D
^ %5E
< %3C
> %3E
Note:均为英文下,中文下不考虑
问题:使用JSON.parse(decodeURIComponent(JSON.stringify(body)))报错 ——Uncaught SyntaxError: Unexpected token / in JSON at position 150
原因:ECMA script注明json字符串中需要转义的字符: " / \ b f n r t
思路:
1、JSON.stringify()之后替换特殊字符
2、decodeURIComponent()之后替换特殊字符
解决:
经验证,特殊字符需要替换为“\特殊字符”,再使用JSON.parse()可以解决。
1、\必须最先替换,防止多余替换其他特殊字符之前的\;
2、"必须在JSON.stringify()之后替换,若在decodeURIComponent()之后替换,会将json结构属性,和属性名的"也替换;
3、\和b f n r t结合,JSON.parse()也会报错,但单独存在不会被编码。
JSON.parse(decodeURIComponent(
JSON.stringify(body).replace(/%5C/g,'%5C%5C')
.replace(/%22/g,"%5C%22")
.replace(/%2F/g,'%5C%2F')
.replace(/%08/g,'%5Cb')
.replace(/%0C/g,'%5Cf')
.replace(/%0A/g,'%5Cn')
.replace(/%0D/g,'%5Cr')
.replace(/%09/g,'%5Ct')));
注:
body =
{
"code": 0,
"message": "success",
"data": {
"offeringBasicList": [{
"offeringId": 2019000167,
"classifyId": 1000000009,
"classifyName": "IOT",
"offeringCode": "%3B%3C%3E%27%22%2F%5C%40%23%24%25%5E%26*%28%29%5B%5Dabcd",
"offeringName": "%3C%3E%27%22%2F%5C%40%23%24%25%5E%26*%28%29%5B%5Dabcd",
"offeringShortName": "%3C%3E%27%22%2F%5C%40%23%24%25%5E%26*%28%29%5B%5Dabcd",
"offeringDesc": "%3C%3E%27%22%2F%5C%40%23%24%25%5E%26*%28%29%5B%5Dabcd",
"isBundled": "N",
"ownerPartyRole": "CA",
"ownerPartyId": "0ff08435-aa1b-49d5-9780-5384d4989c9e",
"releasePartyRole": null,
"releasePartyId": null,
"isPrimary": "Y",
"maxNum": null,
"minNum": null,
"beId": "101",
"orgId": null,
"createdBy": "0ff08435-aa1b-49d5-9780-5384d4989c9e",
"createdTime": 1526623849854,
"updatedBy": "0ff08435-aa1b-49d5-9780-5384d4989c9e",
"updatedTime": 1526623849854,
"thumbnailName": null,
"thumbnailDesc": null,
"thumbnailUrl": null,
"busiModeType": null,
"status": {
"key": "DRA",
"value": "Draft"
},
"ownerType": {
"key": "S",
"value": "Subscriber"
}
}],
"pageInfo": {
"beginRowNumber": 0,
"sortField": "created_time",
"totalRecord": 1
}
}
}
扩展:
1、URI标准不允许使用保留字符,如/,解决:
encodeURIComponent() 编码
decodeURIComponent() 解码
既可编码保留字符,又可编码多字节字符
2、decodeURIComponent()解码包含'%'的字符串时,若%与之后的字符无法转义为正常字符串会报错,eg:'90%';
JSON.parse()——Uncaught SyntaxError: Unexpected token \ in JSON at position 1的更多相关文章
- Uncaught SyntaxError: Unexpected token ' in JSON at position 1
听说js是一样很BT的语言,今天真是有点领教到了. 用python3.6+django2.0开发网站时,遇到了一个坑中之坑! 在异步数据提交Ajax的运用中,不免在回调函数中使用到JSON.parse ...
- uncaught syntaxerror unexpected token U JSON
uncaught syntaxerror unexpected token U JSON The parameter for the JSON.parse may be returning nothi ...
- Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)
这个问题在之前做项目时碰到过一次,当时按照网上的做法,去掉JSON.parse()这一层转换后就没有这个报错了,数据也能正常使用,就没多想,也没深究是什么原因.可是这次又碰到了,所以这次我必须要弄明白 ...
- 关于Uncaught SyntaxError: Unexpected token o in JSON at position 1,chrome持续报错的相关解析
今天跟大家分享我前两天遇见的一个BUG,说出来很难受,因为这个BUG花了我一个多小时去找原因,后来莫名其妙的故障消失了,强迫症犯了的我,居然花了2个多小时去故意再制造这个BUG,只想弄明白WHY??? ...
- VM603:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
再用JQuery解析json的时候出现了这样一个问题 VM603: Uncaught SyntaxError: Unexpected token o 通过查阅资料发现,是由于解析json文件的时候解析 ...
- json格式字符串用Uncaught SyntaxError: Unexpected token ' Uncaught SyntaxError: Unexpected number
Unexpected number(index)的错误用的json字符串如 var jsonStr = "{1:'北京note备注信息',2:'上海note备注信息',3:'广东note备注 ...
- Atitit Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0
Atitit Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0 Uncaught (in ...
- SyntaxError: Unexpected token ' in JSON at position 2
js中字符串转json对象时报错: Uncaught SyntaxError: Unexpected token s in JSON at position 2 解决方法: js中获取jsp的返回值 ...
- [Vue warn]: Error in render: "SyntaxError: Unexpected token ' in JSON at position 1"
一,场景: 字符串转对象: var str = "{'bankRate':5,'YINGUO':0}" 二,操作: JSON.parse(str)时候,报错 [Vue warn]: ...
随机推荐
- 什么是云?Iaas,Paas和SaaS
周围的朋友听说我是做云相关的,总是爱问啥是云?别不是虚幻的概念吧.云计算当然不是虚幻的概念,“云”其实是互联网的一个隐喻,简单地说,云计算是通过Internet(“云”)交付计算服务——服务器.存储. ...
- 音视频处理概要 markdown
最近要想办法把录制的音视频进行拼接. 比方说此次录制的视频有三段,通过高清直播编码器录制,录制下的标准为h.264 直接用ffmpeg简单拼接,音频会丢失,所以有了此次解决方案(有可能会繁琐,简单方案 ...
- keepalived vip 没有生成或者生成了ping不通?
1 问题现象:keepalived已启动但vip 没有生成./var/log/messages日志不断刷屏 tail /var/log/messages Nov :: cache-redis- Kee ...
- 我们一起踩过的坑----react(antd)(一)
1.}] && ){ ){ ){ ||){ ){ );); , }; }); }, beforeUpload: (file) => { ...
- c++学习路线连接
https://blog.csdn.net/qq_36482772/article/category/7396881/4?
- Exp4 恶意代码分析 20165110
Exp4 恶意代码分析 20165110 一.实践目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生 ...
- CentOS7 安装并配置MySQL8.0
安装环境 linux版本:CentOS 7.6 x64 Mysql:8.0 在CentOS中默认安装有MariaDB,但是我们需要的是MySQL,安装MySQL可以覆盖MariaDB. MariaDB ...
- ERROR: 9-patch image C:\...\res\drawable\appwidget.9.png malformed. Frame pixels must be either solid or transparent (not intermediate alphas).
this is the problem with latest adt that is 20.0.3. you can instead rename the *.9.png to *.png and ...
- docker mysql 主主同步
转发自:https://blog.csdn.net/money9sun/article/details/85099134 第一步:安装docker https://www.cnblogs.com/ ...
- VS2008打上SP1的补丁后安装了silverlight后出现未将对象引用设置到对象实例的解决办法
新建silverlight项目失败,提示 未将对象设置到实例 的解决办法: 1.打开 visual studio 命令提示 输入一下命令:2.devenv /resetskippkgs ,这条命令会启 ...