js解析JSON报错:SyntaxError: Unexpected token
来源:互联网 发布:米兰理工大学知乎 编辑:程序博客网 时间:2024/06/06 21:04
问题复现
> node> j = '{a:1,b:"2"}';> JSON.parse(j)SyntaxError: Unexpected token a at Object.parse (native) at repl:1:6 at REPLServer.defaultEval (repl.js:252:27) at bound (domain.js:287:14) at REPLServer.runBound [as eval] (domain.js:300:12) at REPLServer.<anonymous> (repl.js:417:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8)
问题原因
示例中的j不是标准的JSON字符串,键值都得带上引号。
问题解决
使用eval
try{ var json = JSON.parse(j);} catch(e) { var json = eval("("+ j +")");}
参考资料
http://www.json.org/json-zh.html
阅读全文
0 0
- js解析JSON报错:SyntaxError: Unexpected token
- js报错,Uncaught SyntaxError: Unexpected token }
- Js报错Uncaught SyntaxError: Unexpected token <
- JS 报错 “Uncaught SyntaxError: Unexpected token o”
- js报错:Uncaught SyntaxError: Unexpected token ILLEGAL
- node.js报错 SyntaxError: Unexpected token ILLEGAL
- js 使用eval函数 报错uncaught syntaxerror: unexpected token :
- 引用JS报错:uncaught syntaxerror: unexpected token <
- myctrl.js报错: Uncaught SyntaxError: Unexpected token :
- 关于Uncaught SyntaxError- Unexpected token o in JSON at position 1,chrome持续报错的相关解析
- 报错提示:Uncaught SyntaxError: Unexpected token )
- js获取富文本编辑器的值之后报错Uncaught SyntaxError: Unexpected token 发 in JSON at position 0
- JS报错:Uncaught SyntaxError: Unexpected identifier
- js报错:uncaught syntaxError:unexpected tokenj
- 【error】jQuery.ajax()报错Uncaught SyntaxError: Unexpected token
- JSONP跨域问题报错 “Uncaught SyntaxError: Unexpected token :”
- 跨域请求报错 Uncaught SyntaxError: Unexpected token :
- 报错:Error:SyntaxError main.jsx:Unexpected token(3:9)
- Redis(二)
- debian9 安装php7 mysql5.6 nginx docker
- idea部署tomcat及根据数据库表生成实体类
- 关于group by 两个或以上条件的分析
- Log4j配置
- js解析JSON报错:SyntaxError: Unexpected token
- css
- 接口回调
- APP缓存文件视图控制器
- 调用父类同名方法
- HTTP头域列表与解释 之 request篇
- 实现网页多人聊天室 Socket.IO
- Mongodb的安装
- 路由算法与收敛路由原理