Detailed explanation of json file writing format

Detailed explanation of json file writing format

What is JSON

JSON (JavaScript Object Notation) is a data exchange format.

Why this technology?

Before Json, everyone used XML to transfer data. XML is a plain text format, so it is suitable for exchanging data on the Internet, but the XML format is relatively complex. Finally, Douglas Crockford invented JSON, an ultra-lightweight data exchange format.

How to use JSON

- Data format

JSON is actually a subset of JavaScript, so the data format of JSON corresponds to JavaScript:

- number => JS number 
- boolean => JS boolean
- string => JS string //
- null => JS null 
- array => JS Array expression []
- object => JS {} expression

- Notes

  • JSON specifies that the character set is UTF-8, strings must use "", and Object keys must also use ""
  • The last member of an array or object cannot have a comma

- JS has two built-in Json methods

JSON.stringify( {} , [ ] , "")
//Parameter 1: data to be serialized (object)
//Parameter 2: Control the key value of the object. If you only want to output the specified attributes, pass in an array. //Parameter 3: After serialization, print the output format (a Tab to view json more intuitively)

Any conversion of JavaScript to Json means serializing the object into a Json string before it can be transmitted over the network;

Receive a string in Json format.

JSON.parse( json.DATA ) // Pass in the json string

If we receive a string in JSON format, we just need to deserialize it into a JavaScript object and then use it directly in JavaScript.

Examples:

1. Array method [ ]

[{
"id" : 1 ,
"name" : "xiaoming"
},{
"id" : 2 , 
"name" : "xiaohong"
}]

2. Object method { }

//Separate the front-end and back-end, it is recommended that the back-end return data to the front-end in the format {
"status" : 0 , //Execution status code "msg" : "SUCCESS", //Description text information, not NULL
"data" :[{ //The array is nested in the object, the array is the returned data,
"id" : 1 ,
"name" : "xiaohong"
},{
"id" : 2,
"name" : "xiaoming"
}]
}

3. Counterexample

{"id" : ox16 } // Illegal, the value must be in decimal {"name" : underfined } // Illegal, no such value [{
"name" : NUll,
"school" : function() {
console.log("This is wrong")
}//Illegal}]//User-defined functions or system built-in functions cannot be used in json

This concludes this article on the detailed explanation of the json file writing format. I hope it will be helpful for everyone’s study, and I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Json format
  • Understanding JSON (JavaScript Object Notation) in one article
  • JSON Introduction and Usage Summary
  • Detailed introduction to json objects in js
  • jQuery JSON parsing example
  • JSON principle analysis and example introduction
  • Introduction to JSON data format
  • Concise JSON Introduction
  • A brief introduction to json
  • Json advantages and disadvantages and usage introduction

<<:  Solution to the problem of passing values ​​between html pages

>>:  Submit the image through the container DockerCommit and push the image DockerPush

Recommend

Some references about colors in HTML

In HTML, colors are represented in two ways. One i...

Solution for Nginx installation without generating sbin directory

Error description: 1. After installing Nginx (1.1...

How to set the default value of a MySQL field

Table of contents Preface: 1. Default value relat...

What qualities should a good advertisement have?

Some people say that doing advertising is like bei...

Methods and steps to access Baidu Maps API with JavaScript

Table of contents 1. Baidu Map API Access 2. Usin...

Building a Redis cluster on Docker

Table of contents 1. Pull the image 2. Create a R...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

Analysis and solution of a.getAttribute(href,2) problem in IE6/7

Brief description <br />In IE6 and 7, in a ...

Detailed explanation of the basic knowledge of front-end componentization

Table of contents Basic concepts of components Th...

vite2.x implements on-demand loading of ant-design-vue@next components

1. Use version vite:2.0 ant-design-vue: 2.0.0-rc....

Solution to the paging error problem of MySQL one-to-many association query

The query data in the xml price inquiry contains ...

Implementation of CSS loading effect Pac-Man

emmm the name is just a random guess 2333 Preface...