Detailed explanation of Json, learn it in 5 minutes

Hello everyone, welcome to the Stop Refactoring channel

This term I read thisJson.

​Json is an excellent text data format. In many of the technical designs we introduce, we tend to use Json as the data format.

We will start the discussion from the following points:

1. Introduction to JSON

2. Detailed explanation of Json specification

3. Json application scenarios

Json introduction

First introduce Json,Json is a text data format, derived from the object syntax of the programming language JavaScript.

But in essence, Json is independent of JavaScript, and there is no strong connection between the two.

Simply put,Json is a text specification, or a string specification, as shown in the picture Text that conforms to the Json specification.

ThenJsonWhat are the benefits of this text specification Woolen cloth?

The advantage of Json is that it can simply and flexibly represent tree-structured data through a simple A string can record multi-layer structured data.

Of course, the text specification that can record tree data is not only Json, XML. ​ is also possibleThis text data format

And in most cases, XML and Json can be converted to each other.

When the text is not large and the structure is not complex, Json is more concise and easier to read.

However,when the text is particularly large or the structure is particularly complex, XML It is better in streaming processing and readability. For example, HTML and SVG are essentially built on XML.

In addition, it needs to be made clear thatJson text is essentially a string.

So when using Json data in the program, needs to be converted through a specific method can only be used if it is the corresponding object variable.

Taking JavaScript as an example, Json strings need to be converted into JavaScript object variables before they can be used. JavaScript object variables can also be converted into Json strings.

So,Json is just a text specification. As for the Json objects and Json variables in the program, they vary according to different programming languages ​​and different Json Libraries will have differences in operation and expression.

And not all programming languages ​​natively support Json objects. Some programming languages ​​need to introduce third-party Json libraries to use them.

Detailed explanation of Json specification

The following is a detailed introduction to the Json specification.Json supports 6 types of values.

​arestring, number, Boolean value, null, object, array respectively.

Let’s first explain the 4 common value types.

String is a number of characters enclosed in double quotes.

If the string contains double quotes, you need to use \ escape.

Number can be an integer or a floating point number (decimal).

Boolean value is true or false​.

nullempty.

These 4 typesRegular type values ​​are not allowed to embed other values, so the Json text can only contain a string or a number.

Then there are 2 value types that allow other values ​​to be embedded

Object is a dictionary of key/value pairs, or can be understood as a hash table.

needs to be contained within a pair{}, and multiple key-value pairs within a single object are separated by commas.

The key of the . values ​​within a single object do not need to be of the same type​and multiple value can be Json supported Any one of the 6 values,, and the key-value pair must be a string

The array needs to be contained within a pair[]. Multiple arrays in a single array Values ​​are separated by commas.

The value in the array can be any of the 6 values ​​supported by Json,and multiple values ​​in a single array. Values ​​do not need to be of uniform type

Benefited fromobject and array, two value types that allow embedding, a>. Json text can represent complex tree data

In order to improve the scalability of Json text, objects or arrays are generally used as the outermost structure.

So the vast majority of Json text we see is contained in a pair of [] or {}.

The length and embedded layer depth of arrays or objects are not limited in the Json standard, but some Json libraries may have length restrictions.

In addition, extra spaces and line breaks in Json text will not have any substantial impact, justFor the convenience of reading.

Json application scenarios

Next is the application scenario of Json, as mentioned above.

Json text specification is generally suitable for use when the amount of data is not large, data in a tree structure needs to be recorded, or data needs to be flexibly expanded.

We are accustomed to using Json text in back-end interface parameters/return data, cloud computing tasks, configuration files and other scenarios.

Use Json objects as data pools in programs, use Json text to record program logic in our low-code kernel, and more.

In many scenarios, we give priority to Json, which is simple, flexible and easy to read.

Some document databases, such as ElasticSearch, also have data in Json format.

Of course,Json is often just a choice and is not necessarily the optimal solution for the current scenario.

Moreover, many people think that because Json is very flexible and Json variables can add data at will without explicit definition, they think that when the project is large and there are many developers, it will make the code more confusing and difficult to maintain.

It's impossible to argue with that, but here's what we want to say.

In fact, any technical tool will not directly affect the quality of the project. Tools will only affect production efficiency.

What affects the quality of the project is the project process, while what constrains the project process is the norms that most people look down upon a>

We have discussed it in detail when we discussed the front-end and back-end architecture earlier.

If the specifications are incomplete or the team cannot abide by them, the code will be messy and difficult to maintain no matter what tools are used.

Summarize

Finally, although Json is not a framework or tool. It is just a design, but it can express complex tree structure data simply, directly and flexibly.

Although many programming languages ​​also have similar object variables, Json is just a simplified version of them.

But I'm still amazed by the design.

Changes in expression may cause some qualitative changes.

An excellent low-code expression method will bring about a qualitative change in programming efficiency.

This is something we have been exploring in recent years.

Guess you like

Origin blog.csdn.net/Daniel_Leung/article/details/134957160