当前位置:网站首页>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。
边栏推荐
- leetcode 1818 绝对值,排序,二分法,最大值
- Analyze datahub, a new generation metadata platform of 4.7K star
- Home online shopping project
- Nacos
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- Introduction to EtherCAT
- Use of comment keyword in database
- Cookie&Session
- 线程数据共享和安全 -ThreadLocal
- multiple linear regression
猜你喜欢

TEC: Knowledge Graph Embedding with Triple Context

Basic concepts of database

雪崩问题以及sentinel的使用

Hal library setting STM32 interrupt

4、【WebGIS实战】软件操作篇——数据导入及处理

E15 solution for cx5120 controlling Huichuan is620n servo error
![[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation

实现pow(x,n)函数

Edge Drawing: A combined real-time edge and segment detector 翻译

How to achieve 0 error (s) and 0 warning (s) in keil5
随机推荐
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
Gorilla/mux framework (RK boot): RPC error code design
How to use hybrid format to output ISO files? isohybrid:command not found
Go tool cli for command line implementation
Blueprism registration, download and install -rpa Chapter 1
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
Ultimate dolls 2.0 | encapsulation of cloud native delivery
静态库使用MFC和共享库使用MFC的区别
Cookie&Session
Hello World generation
Complete knapsack problem
10、Scanner. Next() cannot read spaces /indexof -1
Subnet division (10)
Detailed list of errors related to twincat3 ads of Beifu
服务器渲染技术jsp
How do spark tasks of 10W workers run? (Distributed Computing)
终极套娃 2.0 | 云原生交付的封装
Research on target recognition and tracking based on 3D laser point cloud
Design of serial port receiving data scheme