当前位置:网站首页>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);
边栏推荐
- bellman-ford AcWing 853. 有边数限制的最短路
- Intel 内部指令 --- AVX和AVX2学习笔记
- 计数类DP AcWing 900. 整数划分
- The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
- LeetCode—剑指 Offer 37、38
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- Efficiency comparison between ArrayList and LinkedList
- Performance tuning project case
猜你喜欢

China traffic sign detection data set

Is the neural network (pinn) with embedded physical knowledge a pit?

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

MySQL and PostgreSQL methods to grab slow SQL

包管理工具

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

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

spfa AcWing 851. spfa求最短路

Simple understanding of ThreadLocal

JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
随机推荐
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
软件测试面试题-2022年大厂面试题合集
通过反射执行任意类的任意方法
LeetCode—剑指 Offer 37、38
Shutter encapsulated button
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Go learning notes - multithreading
spfa AcWing 852. spfa判断负环
Leetcode - Sword finger offer 59 - I, 59 - II
Introduction to CPU instruction set
. Net, C # basic knowledge
Intel internal instructions - AVX and avx2 learning notes
Is the neural network (pinn) with embedded physical knowledge a pit?
怎样写一篇赏心悦目的英文数学论文
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
模块化 CommonJS ES Module
区间DP AcWing 282. 石子合并