当前位置:网站首页>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);
边栏推荐
- Input a three digit number and output its single digit, ten digit and hundred digit.
- Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
- Drools executes the specified rule
- MySQL与PostgreSQL抓取慢sql的方法
- Intel 内部指令 --- AVX和AVX2学习笔记
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- CDA data analysis -- Introduction and use of aarrr growth model
- FastDateFormat为什么线程安全
- Post request body content cannot be retrieved repeatedly
- 堆(優先級隊列)
猜你喜欢

Writing method of then part in drools

Map and set

Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately

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

测试左移和右移
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

刷题---二叉树--2

ThreadLocal的简单理解

堆(优先级队列)

趣味 面试题
随机推荐
Jenkins voucher management
Input box assembly of the shutter package
Simple understanding of ThreadLocal
子线程获取Request
[FFH] little bear driver calling process (take calling LED light driver as an example)
Drools executes string rules or executes a rule file
Leetcode209 长度最小的子数组
MySQL and PostgreSQL methods to grab slow SQL
Mysql database foundation
Tas (file d'attente prioritaire)
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
In development, why do you find someone who is paid more than you but doesn't write any code?
浏览器存储方案
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
LeetCode—剑指 Offer 51. 数组中的逆序对
Leetcode739 每日温度
高德地图测试用例
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
Lombok common annotations
Drools executes the specified rule