当前位置:网站首页>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);
边栏推荐
- Writing method of then part in drools
- 获取文件版权信息
- Enhance network security of kubernetes with cilium
- About asp Net MVC project in local vs running response time is too long to access, the solution!
- Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
- Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
- Mongodb redis differences
- Docker-compose配置Mysql,Redis,MongoDB
- Use MySQL events to regularly perform post seven world line tasks
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
猜你喜欢

浏览器存储方案

VLAN experiment

spfa AcWing 852. spfa判断负环

C#运算符

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime

AI中台技术调研

通过反射执行任意类的任意方法

Performance tuning project case

Redis bloom filter

What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
随机推荐
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
百款拿来就能用的网页特效,不来看看吗?
Leetcode - Sword finger offer 51 Reverse pairs in an array
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
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
线性DP AcWing 902. 最短编辑距离
Sse/avx instruction set and API of SIMD
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
堆 AcWing 839. 模拟堆
包管理工具
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
China traffic sign detection data set
Deep copy event bus
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
BOM DOM
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
AI中台技术调研
bellman-ford AcWing 853. 有边数限制的最短路