Ajax & Json 3. The introduction of Json

1. What is Json

Before introducing Json, let me talk about another thing, which is the data exchange format.

When we first exchanged data through programs, we actually used XML. As time goes by, XML has gradually become bored by people, because the XML format is more laborious, and the parsing is also laborious, requiring disassembly and assembly, so people wonder if there is Other methods or data interfaces are suitable for data transmission.

At this time, Json appeared~~

Go directly to Wikipedia

Two, Json format

Json stands for JavaScript Object Notation, which is a lightweight data exchange format (relative to XML)

Json has its own set of formats

The outermost part of Json is composed of {} braces, and there are many data in the form of key-value pairs inside, for example:

{"name":"CSDN"}、{ "a": 1, "b": [1, 2, 3]}

Or the outermost is composed of the brackets [], inside is the set of storage, for example:

[1, 2, "3", { "a": 4}]、[]

There is nothing, just a number or string

3.14、"plain_text"

These are the correct data in Json format

Moreover, the value in Json can be an object, an array, a number, a string, or one of three literal values ​​(false, null, true).

Another point is that the kv stored in Json is unique to k. If two k are the same, then the value of the latter will overwrite the previous value. We will do exercises to verify this later

Three, how to verify the Json data format

There are several ways to verify the format here

1. Look at it with the naked eye (Of course, you can see if the amount of data is small, once the amount of data is huge and complicated, this method is directly GG)

2. Use the tool, open Baidu, and search for Json 

We can use this thing for online formatting

Paste the data you want to format into the box, and then click to verify~

Four, write a Json by yourself

First open our Eclipse, and then in the previous AjaxHelloWorld project, create a JsonTest class, we will learn here~

Let’s first write the simplest data in kv format.

Here you need to use \ for escaping, otherwise, "This is useless, and json must use double quotation marks to identify the string, and single quotation marks cannot be used.

Then run

As you can see, there is no problem. We don’t need to verify this Json, we just need to distinguish it with the naked eye.

Then we write a Json of a collection of students

First of all, there are multiple students in a class, each student has a student ID and a name, which means that we will have multiple student objects in the collection of this class at that time, and each student object has two attributes (here we need to abstract Imagine, and then we start to write)

As you can see, we have generated a very long Json. Let’s check it with a tool. Is this Json correct?

There is no problem. There is a collection in class 1. There are students in class 1. Chen 1, Chen 2, and there are two students in class 2, Chen 3 and Chen 4.

Five, Json and XML comparison

I felt the data in Json format above, and then we are comparing the data in XML format

As you can see, first of all, the first point, not much else, this XML writes more things than Json writes.

Another is readability. Json is a concise version, and this XML is a standard version. Each has its own advantages, so we don’t make too many comparisons.

We now use Json more because of the effective data rate. JSON is more efficient when it is transmitted as a data packet format. This is because JSON does not require strict closing tags like XML, which makes the effective data volume and the total The data packet ratio is greatly improved, thereby reducing the transmission pressure of the network under the same data flow.

Of course, it's not that this XML will not be used in the future, it will be. Some companies, such as SF Express, use the XML format for data transmission, so we all need to learn~~

 

You can check it out for yourself, if you don’t understand, please contact me QQ: 2100363119

Welcome everyone to visit my website: https://www.lemon1234.com

If you can, pay attention to my official account, it is on my website, updated every day~~~, unlimited resources to enjoy Java, thank you~

Recently, the mini program has also been opened. You can scan the code to have fun.

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/113883088
Recommended