当前位置:网站首页>JSON serialization and parsing
JSON serialization and parsing
2022-07-02 12:44:00 【There is no water in the sea】
1、JSON The top level supports three types of values

2、 Basic use
const obj = {
name: "chen",
age: 23,
friends: {
name: "feng",
},
hobbies: [" football "],
};
// We need to use JSON.stringify():
const objstring = JSON.stringify(obj);// It's essentially a json Format
// Store object data localStorage
localStorage.setItem("obj", objstring); // The key requires a string , What we are passing on is the object , Will be converted to a string
console.log(localStorage.getItem("obj")); //[object Object], Unable to restore
3、json serialize
const obj = {
name: "chen",
age: 23,
friends: {
name: "feng",
},
hobbies: [" football "],
};
// demand : Turn the above object into jSON character string
// 1、 Direct conversion
const jsonString1 = JSON.stringify(obj);
console.log(jsonString1); //{"name":"chen","age":23,"friends":{"name":"feng"},"hobbies":[" football "]}
// 2、stringify The second parameter replacer
// 2.1、 Pass in array : Set what needs to be converted . Put that key Will convert
const jsonString2 = JSON.stringify(obj, ["name", "friends"]);
console.log(jsonString2); //{"name":"chen","friends":{"name":"feng"}}
// 2.2、 Incoming callback function , It can be done to key/value Intercept
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":[" football "]}
// 3、stringify The third parameter space
const jsonString4 = JSON.stringify(obj, null, 2);
console.log(jsonString4);
4、json Parse into objects
const JSONString =
'{"name":"chen","age":24,"friends":{"name":"feng"},"hobbies":[" football "]}';
// The second parameter can also be intercepted
const info = JSON.parse(JSONString, (key, value) => {
if (key === "age") {
return value - 1;
}
return value;
});
console.log(info);
边栏推荐
- 分布式机器学习框架与高维实时推荐系统
- Fluent fluent library encapsulation
- Leetcode - Sword finger offer 59 - I, 59 - II
- Introduction to CPU instruction set
- js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
- 防抖 节流
- Leetcode - Sword finger offer 37, 38
- Shutter encapsulated button
- 一些突然迸发出的程序思想(模块化处理)
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
猜你喜欢

应用LNK306GN-TL 转换器、非隔离电源

Docker-compose配置Mysql,Redis,MongoDB

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

Simple understanding of ThreadLocal

The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

Performance tuning project case

Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)

模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计

趣味 面试题
随机推荐
Docsify deploy IIS
8A 同步降压稳压器 TPS568230RJER_规格信息
Do you know all the interface test interview questions?
包管理工具
Docker compose configuration mysql, redis, mongodb
Efficiency comparison between ArrayList and LinkedList
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
What data types does redis have and their application scenarios
Visual studio efficient and practical extension tools and plug-ins
哈希表 AcWing 841. 字符串哈希
Find the common ancestor of any two numbers in a binary tree
. Net, C # basic knowledge
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
[ybtoj advanced training guide] similar string [string] [simulation]
"As a junior college student, I found out how difficult it is to counter attack after graduation."
应用LNK306GN-TL 转换器、非隔离电源
Go learning notes - multithreading
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Drools terminates the execution of other rules after executing one rule
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand