当前位置:网站首页>JSON序列化 与 解析
JSON序列化 与 解析
2022-07-02 09:43:00 【大海里没有水】
1、JSON顶层支持的三种类型的值

2、基本使用
const obj = {
name: "chen",
age: 23,
friends: {
name: "feng",
},
hobbies: ["足球"],
};
// 我们需要使用JSON.stringify():
const objstring = JSON.stringify(obj);// 本质上是一个json格式
// 将对象数据存储localStorage
localStorage.setItem("obj", objstring); // 键要求存的是字符串,我们传的是对象,会被转为字符串
console.log(localStorage.getItem("obj")); //[object Object], 无法还原
3、json序列化
const obj = {
name: "chen",
age: 23,
friends: {
name: "feng",
},
hobbies: ["足球"],
};
// 需求:将上面的对象转成jSON字符串
// 1、直接转化
const jsonString1 = JSON.stringify(obj);
console.log(jsonString1); //{"name":"chen","age":23,"friends":{"name":"feng"},"hobbies":["足球"]}
// 2、stringify第二个参数replacer
// 2.1、传入数组:设定哪些是需要转换的。放入那个key就会转换
const jsonString2 = JSON.stringify(obj, ["name", "friends"]);
console.log(jsonString2); //{"name":"chen","friends":{"name":"feng"}}
// 2.2、传入回调函数,可以对key/value做拦截
const jsonString3 = JSON.stringify(obj, (key, value) => {
// return value;
if (key === "age") {
return value + 1;
}
return value;
});
console.log(jsonString3); //{"name":"chen","age":24,"friends":{"name":"feng"},"hobbies":["足球"]}
// 3、stringify第三个参数 space
const jsonString4 = JSON.stringify(obj, null, 2);
console.log(jsonString4);
4、json解析成对象
const JSONString =
'{"name":"chen","age":24,"friends":{"name":"feng"},"hobbies":["足球"]}';
// 第二个参数同样可以进行拦截
const info = JSON.parse(JSONString, (key, value) => {
if (key === "age") {
return value - 1;
}
return value;
});
console.log(info);
边栏推荐
- There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
- Docker compose configuration mysql, redis, mongodb
- Calculate the maximum path sum of binary tree
- 记录一下MySql update会锁定哪些范围的数据
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- 子线程获取Request
- 5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
- 全链路压测
- Leetcode122 the best time to buy and sell stocks II
- Sort---
猜你喜欢

CDA数据分析——AARRR增长模型的介绍、使用

Tas (file d'attente prioritaire)

Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)

drools决策表的简单使用

Test shift left and right

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes

Adding database driver to sqoop of cdh6

drools动态增加、修改、删除规则

Deep Copy Event bus
随机推荐
drools执行String规则或执行某个规则文件
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
drools动态增加、修改、删除规则
Leetcode922 按奇偶排序数组 II
Map and set
Use sqoop to export ads layer data to MySQL
CPU指令集介绍
JZ63 股票的最大利润
CDA data analysis -- Introduction and use of aarrr growth model
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
"As a junior college student, I found out how difficult it is to counter attack after graduation."
MySQL and PostgreSQL methods to grab slow SQL
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
SSH automatically disconnects (pretends to be dead) after a period of no operation
How to write a pleasing English mathematical paper
Maximum profit of jz63 shares
LeetCode—剑指 Offer 59 - I、59 - II
Gaode map test case
From scratch, develop a web office suite (3): mouse events
[C language] convert decimal numbers to binary numbers