当前位置:网站首页>json. Stringify() and json The difference between parse () and json Usage of stringify()
json. Stringify() and json The difference between parse () and json Usage of stringify()
2022-06-13 06:38:00 【A-linWeb】
Recent projects , Find out JSON.stringify() There are a lot of scenarios , We all know JSON.stringify() The role of the JavaScript Object to JSON character string , and JSON.parse() Can be JSON String to an object .
To put it simply , Their functions are relative , I use JSON.stringify() Put the object a It becomes a string c, Then I can use JSON.parse() The string c Restore to object a
.
let arr = [1,2,3];
JSON.stringify(arr);//'[1,2,3]'
typeof JSON.stringify(arr);//string
let string = '[1,2,3]';
console.log(JSON.parse(string))//[1,2,3]
console.log(typeof JSON.parse(string))//object
In the use of JSON.parse() One thing to note , Because this method is JSON Converts a string to an object , So your string must match JSON Format , That is, the key value must be wrapped in double quotation marks :
let a = '["1","2"]';
let b = "['1','2']";
console.log(JSON.parse(a));// Array [1,2]
console.log(JSON.parse(b));// Report errors
Two 、JSON.stringify() Several wonderful uses of
1. Determine whether the array contains an object , Or judge whether the objects are equal .
// Determine whether the array contains an object
let data = [
{
name:'echo'},
{
name:' Listening to the wind is the wind '},
{
name:' The emperor laughs '},
],
val = {
name:' The emperor laughs '};
JSON.stringify(data).indexOf(JSON.stringify(val)) !== -1;//true
// Judge two arrays / Whether objects are equal
let a = [1,2,3],
b = [1,2,3];
JSON.stringify(a) === JSON.stringify(b);//true
2. Give Way localStorage/sessionStorage Can store objects .
localStorage/sessionStorage By default, only strings can be stored , But in actual development , Most of the data we need to store are object types , So here we can use... In storage json.stringify() Convert object to string , When fetching the cache , Just cooperate json.parse() Just turn back to the object .
// save
function setLocalStorage(key,val){
window.localStorage.setItem(key,JSON.stringify(val));
};
// take
function getLocalStorage(key){
let val = JSON.parse(window.localStorage.getItem(key));
return val;
};
// test
setLocalStorage('demo',[1,2,3]);
let a = getLocalStorage('demo');//[1,2,3]
3. Implement object deep copy
In development , If you are afraid of affecting the original data , We often copy out a copy of data and do any operation , Actually use JSON.stringify() And JSON.parse() To realize deep copy is a good choice .
// Deep copy
function deepClone(data) {
let _data = JSON.stringify(data),
dataClone = JSON.parse(_data);
return dataClone;
};
// test
let arr = [1,2,3],
_arr = deepClone(arr);
arr[0] = 2;
console.log(arr,_arr)//[2,2,3] [1,2,3]
3、 ... and 、JSON.stringify() And toString() The difference between
let arr = [1,2,3];
JSON.stringify(arr);//'[1,2,3]'
arr.toString();//1,2,3
secondly ,JSON.stringify() The target audience is more , and toString() Although you can turn an array into a string , But not to {name:‘ Arlene ’} This kind of object implements the operation you want , Its audience is more digital .
So maybe so much has been sorted out , If you encounter problems in later work, please add them again .
边栏推荐
- 'ipconfig' is not an internal or external command, nor is it a runnable program or batch file.
- 端午安康,使用祝福话语生成词云吧
- Jetpack - basic use of room
- Solutions to common problems in small program development
- MFS details (VII) -- MFS client and web monitoring installation configuration
- Usegeneratedkeys=true configuration
- Ijkplayer code walk through read_ AV in thread thread_ read_ Detailed explanation of frame() data stream reading process
- RN Metro packaging process and sentry code monitoring
- 时间格式化工具----moment.js(网页时间实时展示)
- The jadx decompiler can decompile jars and apks
猜你喜欢

The web server failed to start Port 7001 was already in use

JetPack - - - DataBinding

景联文科技提供一站式智能家居数据采集标注解决方案

In kotlin?,!,?:,:, - & gt;、== Brief description of symbols

【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】

【Kernel】驱动编译的两种方式:编译成模块、编译进内核(使用杂项设备驱动模板)

无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制
![[solution] camunda deployment process should point to a running platform rest API](/img/ef/5b893e9c315c10db6c1db46b4c3f5a.jpg)
[solution] camunda deployment process should point to a running platform rest API

Solution: vscode open file will always overwrite the last opened label

MFS详解(七)——MFS客户端与web监控安装配置
随机推荐
楊輝三角形詳解
SSM框架整合--->简单后台管理
Basic knowledge of knowledge map
Solutions to common problems in small program development
RN Metro packaging process and sentry code monitoring
机器学习笔记 - 监督学习备忘清单
Wechat applet uploads pictures (preview deletion limits the size and number of pictures)
Analysis of 43 cases of MATLAB neural network: Chapter 10 classification of discrete Hopfield Neural Network -- evaluation of scientific research ability of colleges and Universities
SSM integration
[solution] camunda deployment process should point to a running platform rest API
The processing and application of C language to documents
Session and browser
Kotlin collaboration channel
Cocos creator compilation game cannot read property 'polygonpolygon' of undefined
Kotlin collaboration process +flow download case
Learning records countless questions (JS)
Recyclerview has data flicker problem using databinding
MFS explanation (VI) -- MFS chunk server installation and configuration
Kotlin data flow - flow
景联文科技提供一站式智能家居数据采集标注解决方案