当前位置:网站首页>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);
边栏推荐
- 深拷贝 事件总线
- drools动态增加、修改、删除规则
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
- 子线程获取Request
- Post request body content cannot be retrieved repeatedly
- Record the range of data that MySQL update will lock
- Mysql database foundation
- Performance tuning project case
- Calculate the maximum path sum of binary tree
- Drools executes string rules or executes a rule file
猜你喜欢
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
Jenkins voucher management
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
初始JDBC 编程
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
堆(優先級隊列)
Jenkins用户权限管理
浏览器存储方案
drools动态增加、修改、删除规则
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
随机推荐
Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
趣味 面试题
全链路压测
刷题---二叉树--2
单指令多数据SIMD的SSE/AVX指令集和API
Leetcode - Sword finger offer 37, 38
计算二叉树的最大路径和
Leetcode14 longest public prefix
In development, why do you find someone who is paid more than you but doesn't write any code?
中国交通标志检测数据集
Leetcode - Sword finger offer 59 - I, 59 - II
CDA数据分析——Excel数据处理的常见知识点归纳
深拷贝 事件总线
Go learning notes - go based interprocess communication
Docker-compose配置Mysql,Redis,MongoDB
Sse/avx instruction set and API of SIMD
Sort---
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
CDH6之Sqoop添加数据库驱动
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60