The difference and use of json.stringify() and json.parse()

The difference and use of json.stringify() and json.parse()

1. Differences between JSON.stringify() and JSON.parse()

We have all used JSON.stringify() and JSON.parse(). You can tell from the names
JSON.stringify() is used to convert a JavaScript object into a JSON string.
JSON.parse() can convert a JSON string into an object.

Easy-to-understand version:

  • JSON.stringify() converts object a into string s;
  • JSON.parse() converts string s into object a;

To put it simply, their functions are relative. If I use JSON.stringify() to convert object a into string c, then I can use JSON.parse() to restore string c into 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

One thing to note when using JSON.parse() is that since this method converts a JSON string into an object, your string must conform to the JSON format, that is, both keys and values ​​must be wrapped in double quotes:

let a = '["1","2"]';
let b = "['1','2']";
console.log(JSON.parse(a)); // Array [1,2]
console.log(JSON.parse(b)); // Error

2. Useful Uses of JSON.stringify()

1. Determine whether an array contains an object, or whether the objects are equal.

//Judge whether the array contains an object let data = [
  {name:'Nuggets'},
  {name:'css learning'},
  {name:'js learning'},
  ],
  val = {name:'Nuggets'};
JSON.stringify(data).indexOf(JSON.stringify(val)) !== -1; // true

//Judge whether two arrays/objects are equal let a = [1,2,3],
  b = [1,2,3];
JSON.stringify(a) === JSON.stringify(b); //true

2. Allow localStorage/sessionStorage to store objects.

By default, localStorage/sessionStorage can only store strings. In actual development, we often need to store objects. In this case, we can use json.stringify() to convert objects into strings when storing them. When retrieving the cache, we only need to use json.parse() to convert them back into objects.

//Storage function setLocalStorage(key,val){
  window.localStorage.setItem(key,JSON.stringify(val));
};
//Get function getLocalStorage(key){
  let val = JSON.parse(window.localStorage.getItem(key));
 window.localStorage.removeItem(key)
  return val;
};
//Test let data = [
  {name:'Nuggets'},
  {name:'css learning'},
  {name:'js learning'},
  ];
setLocalStorage('STORAGEDATE',data);
let a = getLocalStorage('STORAGEDATE'); 

3. Implement deep copy of objects

In actual development, if we are afraid of affecting the original data, we often make a deep copy of the data for arbitrary operations. In fact, using JSON.stringify() and JSON.parse() to achieve 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]

This concludes this article about the differences and uses of json.stringify() and json.parse(). For more information about json.stringify() and json.parse(), please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the performance test of JSON.parse() and JSON.stringify()
  • JS uses JSON.parse() and JSON.stringify() to implement deep copy function analysis of objects
  • JSON.parse function and JSON.stringify function in JavaScript
  • Dynamic key setting in JSON and the difference between JSON.parse and JSON.stringify()
  • A brief discussion on the differences between JSON.stringify() and JOSN.parse()
  • About the usage of JSON.parse(), JSON.stringify(), jQuery.parseJSON()
  • Talk about the conversion between JSON objects and strings JSON.stringify(obj) and JSON.parse(string)
  • A brief discussion on JSON.parse() and JSON.stringify()
  • Introduction to JSON.parse() and JSON.stringify()
  • Detailed explanation of JSON.parse and JSON.stringify usage

<<:  Detailed process of decompressing and installing mysql5.7.17 zip

>>:  MySQL replication table details and example code

Recommend

How to update Ubuntu 20.04 LTS on Windows 10

April 23, 2020, Today, Ubuntu 20.04 on Windows al...

Quickly master the use of Docker to build a development environment

As the platform continues to grow, the project...

Vue application example code based on axios request encapsulation

Table of contents What is axios? Axios request ty...

Nine advanced methods for deduplicating JS arrays (proven and effective)

Preface The general methods are not listed here, ...

MySQL Basics Quick Start Knowledge Summary (with Mind Map)

Table of contents Preface 1. Basic knowledge of d...

Using Docker run options to override settings in the Dockerfile

Usually, we first define the Dockerfile file, and...

The difference between HTML iframe and frameset_PowerNode Java Academy

Introduction 1.<iframe> tag: iframe is an i...

How to use video.js in vue to play m3u8 format videos

Table of contents 1. Installation 2. Introducing ...

JavaScript function syntax explained

Table of contents 1. Ordinary functions 2. Arrow ...

JavaScript implements asynchronous submission of form data

This article example shares the specific code of ...

MySQL data compression performance comparison details

Table of contents 1. Test environment 1.1 Hardwar...

JavaScript form validation example

HTML forms are commonly used to collect user info...