当前位置:网站首页>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);
边栏推荐
- Redis sentinel mechanism and configuration
- Day12 control flow if switch while do While guessing numbers game
- Intel 内部指令 --- AVX和AVX2学习笔记
- Leetcode - Sword finger offer 37, 38
- 线性DP AcWing 895. 最长上升子序列
- Introduction to CPU instruction set
- There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- BOM DOM
- LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
猜你喜欢
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
bellman-ford AcWing 853. 有边数限制的最短路
Embedded Software Engineer career planning
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Enhance network security of kubernetes with cilium
arcgis js 4. Add pictures to x map
AI mid stage technology research
随机推荐
包管理工具
单指令多数据SIMD的SSE/AVX指令集和API
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
线性DP AcWing 896. 最长上升子序列 II
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Floyd AcWing 854. Floyd求最短路
AI中台技术调研
浏览器node事件循环
染色法判定二分图 AcWing 860. 染色法判定二分图
Adding database driver to sqoop of cdh6
Record the range of data that MySQL update will lock
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
LeetCode—剑指 Offer 59 - I、59 - II
软件测试面试题-2022年大厂面试题合集
线性DP AcWing 895. 最长上升子序列
Distributed machine learning framework and high-dimensional real-time recommendation system
About the loading of layer web spring layer components, the position of the layer is centered
一些突然迸发出的程序思想(模块化处理)
绕过ObRegisterCallbacks需要驱动签名方法
1380. Lucky numbers in the matrix [two-dimensional array, matrix]