This is the 19th day of my participation in the August More Text Challenge

Persistence in learning, notes is the soul, review the old to learn new, from time to time, review the knowledge point, deepen memory, get twice the result with half the effort!

We’ve seen node.js in action: the file system module combined with the HTTP module – building a simple local file server.

By reading the local file, simulating the data interface, fetching the local.json file and returning it, is the most used data structure of the front and back end communication

Let’s take a look at this amazing data structure: JSON

Dig deeper into JSON

Today we’ll talk about JSON, arguably the most popular data interchange format in the world: json.org

JSON(JavaScript Object Notation) is a lightweight data interchange format. Easy to read and write. It is also easy for machine parsing and generation. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

Do you knowJSONYour name?

JSON is a common and popular serialization format. Its full name: JavaScript Object Notation

For example, we often deal with JSON, and we are familiar with JavaScript, that is, JS, will have association with it?

The writing format of JSON

JSON looks similar to how we write JavaScript objects and arrays, but JSON has some limitations:

  1. All attribute names must be in double quotes""Enclosed,
  2. And only simple data expressions such as numbers/strings/objects/arrays are allowed, not function calls/anything that involves computation..
  3. Comments are not allowed in JSON…

For example, the format normally returned by an interface in a project contains the corresponding fields:

Here is the raffle page showing the format of the data returned by the prize list interface:

Data used: status “err_msg” returned by the request interface, user name” user_name”, statistics “count”, etc

{
  "data": {
    "count": 544."lotteries": [{"lottery_image": "https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ab31c183950541d4a0731c0b8765b173~tplv-k3u1fbpfcp-no-mark:0:0:0:0.imag e"."lottery_name": "Nuggets of gold Mug."."user_avatar": "https://sf3-ttcdn-tos.pstatp.com/img/user-avatar/0c1e7da2d3aa42f640a3e5574f584eae~300x300.image"."user_name": "The one who loves you the most."}]."err_msg": "success"."err_no": "0"}}Copy the code

JSON data format conversion

JavaScript provides two methods for converting data: json.stringify () and json.parse ().

Json.stringify (value) encodes the retrieved JavaScript value into a JSON-encoded string

Json.parse (the formatted string above) converts the encoded string above and decodes it to its encoded value

Example:

let stringifyString = JSON.stringify({ err_msg: 'success'.data: ['lists']})console.log(stringifyString)
// --> {"err_msg":"success","data":["lists"]}

let parseString = JSON.parse(stringifyString)

console.log(parseString.data) // --> ["lists"]
Copy the code

Go deep into its analytical principle

Website: json.org

Here are some pictures from its official website (the domain name bought by the author), and you can see for yourself..

1. Object parsing

2. Array Array parsing

3. Value parsing

4. String parsing

4. Parse the number

4. Parse whitespace

Read more

Read more articles please check:

Day18 file system server – analog interface,

[Node.js] day16-file system module, [Node.js] day17-HTTP module,

【Node.js】 Efficiency tools – NVM & NRM, etc.

Node.js package management tool NPM & YARN

[node.js] Installation & documentation,

【Github】 Multiplayer collaboration (2), 【Github】 Basic use (1),

【Git】 code version control – Basic Operation (1)

[Node.js] Build automation development environment – Basic introduction, [Tool preparation], [Start], [Detailed steps (4)], [module processing tools (5)], [Understanding of modular programming]

Keep up the pace, step by step! Updates below:

Next we will continue to learn node.js related methods in detail, chong Duck!! xdm

Learn the efficiency tools to improve development efficiency and empower our development! Come on!! go~~