JSON.parse() Unexpected token i in JSON at position 2 报错问题
来源:互联网 发布:c语言中flag 编辑:程序博客网 时间:2024/06/08 12:39
JSON.parse(): Unexpected token i in JSON at position 2 报错问题
错误代码:
var res = "[{id:1,name:'liming'},{id:2,name:'xiaobai'}]"; var objRes = JSON.parse(res); console.log(objRes)
正确代码:
var res = '[{"id":1,"name":"liming"},{"id":2,"name":"xiaobai"}]'; var objRes = JSON.parse(res); console.log(objRes)
总结:
在使用JSON.parse的时候,返回来的数据一定是严格的json格式,key值一定要用 双引号 包起来,否则浏览器就会抛出异常。
另外,对于不标准的json字符串,如第一种示例,使用eval()虽然可以正常解析,但是从安全的角度来讲,一定要谨慎使用eval方法。规范json格式。
通常我们在处理数据的时候,都是后台直接给我们的。时间久了,就忘了一些最基础的东西,共勉。
阅读全文
0 0
- JSON.parse() Unexpected token i in JSON at position 2 报错问题
- JSON.parse()报错 Unexpected token i in JSON at position 2
- JSON.parse(str)报错Unexpected token t in JSON at position 1
- 模块引用文件路径问题js报错和Unexpected token in JSON at position
- ajax post请求发送json数据报错unexpected token in json at position的问题解决
- Uncaught SyntaxError: Unexpected token ] in JSON at position 问题
- Ajax报错之Unexpected token S in JSON at position
- js常见报错之Unexpected token in JSON at position
- VM356:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
- VM356:1 Uncaught SyntaxError: Unexpected token i in JSON at position 1
- primefaces 推送服务报Uncaught SyntaxError: Unexpected token < in JSON at position 0
- Uncaught SyntaxError: Unexpected token < in JSON at position 0
- SyntaxError: Unexpected token s in JSON at position 111518
- SyntaxError: Unexpected token s in JSON at position 1
- 解决方法 Uncaught SyntaxError: Unexpected token < in JSON at position 0
- 【Angular2】Unexpected token / in JSON at position 35
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- Uncaught SyntaxError: Unexpected token o in JSON at position 1
- learning之适配器模式
- Linear Kernel: Why is it recommended for text classification ?
- Java线程池的分析和使用
- windows下python3.6连接mysql数据库
- Ubantu+Opencv +QT 心累的一周
- JSON.parse() Unexpected token i in JSON at position 2 报错问题
- Go语言操作PostgreSQL数据库——学习笔记
- function返回多个值
- JAVA程序调用Matlab神经网络工具编译的jar包注意问题
- 信用,你最大的财富!——刘强东打出最强王牌!
- 浅谈spring IOC容器中装配bean
- 系统性能基础知识
- 如何在Android Studio中使用Kotlin开发app
- Cesium学习笔记(五)几何和外观