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]: ...
随机推荐
- o2o、c2c、b2c、b2b、b2b2c都是什么?
o2o:线上线下,如线上消费,线下享受服务,o2o是不需要物流的,是线上购买的东西,自己去线下获得.c2c:个人对个人,消费者与消费者之间的小成本交易,买卖双方都不是公司,如你在咸鱼上卖自己的东西.b ...
- python学习Day13 函数的嵌套定义、global、nonlocal关键字、闭包及闭包的运用场景、装饰器
复习 1.函数对象:函数名 => 存放的是函数的内存地址1)函数名 - 找到的是函数的内存地址2)函数名() - 调用函数 => 函数的返回值 eg:fn()() => fn的返回值 ...
- python day04笔记总结
2019.4.1 S21 day04笔记总结 昨日内容补充 1.解释器/编译器 1.解释型语言.编译型语言 2.解释型:写完代码后提交给解释器,解释器将代码一行行执行.(边接收边解释/实时解释) 常用 ...
- Exp2 后门原理与实践 20165110
Exp2 后门原理与实践 一.实验要求 1.使用netcat获取主机操作Shell,cron启动 2.使用socat获取主机操作Shell, 任务计划启动 3.使用MSF meterpreter(或其 ...
- hdu 3065 AC自动机 标记数组不清零
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目里面要我们计算每种单词出现的次数,重叠的也要计算,那么我们在查找的时候不要把标记单词结尾的 ...
- Celery 1
Celery是一个用Python开发的异步的分布式任务调度模块 Celery有以下优点: 简单:一但熟悉了celery的工作流程后,配置和使用还是比较简单的 高可用:当任务执行失败或执行过程中发生连接 ...
- 19. Remove Nth Node From End of List (JAVA)
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 递归可视化之汉诺塔的动画实现(turtle海龟)
import turtle class Stack: def __init__(self): self.items = [] def isEmpty(self): def push(self, ite ...
- python完整课程
python完整课程 python课程概述 python课程大纲 链接:http://www.cnblogs.com/lx63blog/articles/9054294.html 课程结构: 1.安装 ...
- R语言读取XML数据