当前位置:网站首页>Unexpected token o in JSON at position 1 ,JSON解析问题
Unexpected token o in JSON at position 1 ,JSON解析问题
2022-07-01 03:21:00 【家家小迷弟】
跟着视频实现登录的时候,突然报错Unexpected token o in JSON at position 1
看样子应该是解析出错了,但是这里我使用各种console.log调试,都没找到问题所在
从cookie中取出用户信息赋值给对象,那就看看到底赋了个啥:

应该就是这里出问题了,应该是存入的时候就出问题了,格式不对
存的时候是直接存入对象,不是字符串,后面的parse解析方法是将字符串解析成json对象,所以在存的时候,就应该先把对象转换成字符串: cookie.set('guli_ucenter', JSON.stringify(this.loginInfo), { domain: 'localhost' })
就很nice:
下面是JSON.parse()和JSON.stringify()用法:
JSON.parse()用于从一个字符串中解析出json对象,如:
var str = '{"name":"LeonWu","age":"18"}'
JSON.parse(str);
//结果为一个Object
// age: "18";
// name: "LeonWu";
JSON.stringify()用于从一个对象解析出字符串,如
var a = {
a:1,b:2,c:"LeonWu"};
JSON.stringify(a);
//结果为 "{"a":1,"b":2,"c":"LeonWu"}"
报错的原因:要转换的数据原本就是object,这个方法是把一个字符串解析出json对象,再转换就会报错;
由于把Object做为参数传到JSON.parse()里时,它会默把Object转为string,
就会先调用原型上的toString()方法;结果为”[object Object]”,JSON.parse将第一个字符’[‘理解为数组的开始,第二字符’o’不知道怎么处理;因此就抛出了上面的错误信息 Unexpected token o in JSON at position 1。
边栏推荐
- 5. [WebGIS practice] software operation - service release and permission management
- GCC usage, makefile summary
- Binary tree god level traversal: Morris traversal
- Subnet division and subnet summary
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- 二叉树神级遍历:Morris遍历
- Leetcode:829. Sum of continuous integers
- ECMAScript 6.0
- 用小程序的技术优势发展产业互联网
- 在线公网安备案保姆级教程【伸手党福利】
猜你喜欢

pytorch训练深度学习网络设置cuda指定的GPU可见
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

Ctfshow blasting WP

How to achieve 0 error (s) and 0 warning (s) in keil5

Cookie&Session

Addition without addition, subtraction, multiplication and division

TEC: Knowledge Graph Embedding with Triple Context

pytorch nn.AdaptiveAvgPool2d(1)

Avalanche problem and the use of sentinel

Feign远程调用和Getaway网关
随机推荐
Hal library operation STM32 serial port
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
二叉树神级遍历:Morris遍历
Appium自动化测试基础 — APPium基本原理
Nacos
pytorch nn. AdaptiveAvgPool2d(1)
不用加减乘除实现加法
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Addition without addition, subtraction, multiplication and division
文件上传下载
Ultimate dolls 2.0 | encapsulation of cloud native delivery
[us match preparation] complete introduction to word editing formula
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
Explain spark operation mode in detail (local+standalone+yarn)
C语言的sem_t变量类型
go实现命令行的工具cli
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Binary tree god level traversal: Morris traversal
Thread data sharing and security -threadlocal
TEC: Knowledge Graph Embedding with Triple Context