“This is the 27th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Hello, everyone, I touch fish small public, the real strong, will not complain, if you do not want to be looked down upon by others, you only pay more than others ten times a hundred times more efforts, to stand higher than others! The last article described the offsetWidth, clientWidth, scrollWidth, and other properties of JS. Today we are going to take a look at JavaScript JSON data formats.

What is JSON?

JSON(JavaScript Object Notation) is a lightweight data interchange format that is easy to read and write. JSON is a syntax for serializing objects, arrays, values, strings, booleans, and NULL. It’s based on JavaScript syntax, but it’s different: JavaScript isn’t JSON, and JSON isn’t JavaScript. When the front-end requests the interface, the front-end sets the background to return JSON data format.

In JSON, there are two structures: objects and arrays.

Object data format

Var obj = {"name":"Amy", "sex":" female "}Copy the code

The data format of an array

Var arr = [{" name ":" Amy ", "sex" : "f"}, {" name ":" Anna ", "sex" : "f"}, {" name ":" Tom "and" sex ", "male"},]Copy the code

Two ways to convert each other

Js objects to JSON data

Json.stringify () returns the JSON string corresponding to the specified value

Error: Data has special characters. This error is an error commonly reported in this method. It looks like the code is fine, but if you look closely, it’s actually my data with Chinese commas.

Var arr = [{" name ":" Amy ", "sex", "female", the age: 18}, {" name ":" Anna ", "sex", "female", the age: 28}, {" name ":" Tom "and" sex ", "male", age:25} , ] var arrStr=JSON.stringify(arr) console.log(arrStr) //error: Uncaught SyntaxError: Invalid or unexpected tokenCopy the code
Var arr = [{" name ":" Amy ", "sex" : "f"}, {" name ":" Anna ", "sex" : "f"}, {" name ":" Tom," "Sex ":" male "}] var arrStr= json.stringify (arr) console.log(arrStr) // Prints the data as follows / / [{" name ":" Amy ", "sex" : "f"}, {" name ":" Anna ", "sex" : "f"}, {" name ":" Tom "and" sex ", "male"}]Copy the code

JSON converts JS objects

Json.parse () parses the JSON string and returns the corresponding value, as in the example above

Application scenarios

1. Used when passing a parameter as an object through the URL. Json.stringify () is used to pass the data on the current page and json.parse () is used to convert the data received on the next page.

Json.parse () and json.stringify () are used to store data in sessionStorage.

Differences between JSON and javaScript

The difference between JSON JavaScript
meaning It’s just a grid of data Represents an instance of a class
transmission Cross-platform data transmission, fast speed Can’t transfer
performance In key-value pair mode, the key must be enclosed in double quotation marks. The value cannot be a method function and cannot be undefined/NaN. Key-value pair mode, keys are not quoted; Values can be functions, objects, strings, numbers, Boolean, and so on.
transformation Parse () Convert Json to Js objects Json, json.stringify ()

Conclusion:

Well, this is the end of the article, welcome everyone (like + comment + attention) questions can come to exchange; I hope you find this article useful and support me. Today is the 27th day of my participation in the first Wenwen Challenge 2022, come on!