Ajax & Json 4. The use of Json & the use of Ajax and Json

1. The use of Json

Our last article described the basic usage of Json, and we also wrote a few Jsons ourselves.

In the process of writing, it is not difficult to find that this is the problem of the addition of the escape character. If this allows us to write the student data of a class, it will be finished, so in order to prevent us from writing one by one, we Introduce two commonly used Json formatting tools

One is Gson launched by Google, and the other is Fastjson launched by Alibaba

We can use either of these two. It’s up to you to choose. Let me talk about Gson first.

Before that, we create a Student class, we will use it later~~

id primary key

name

createDt creation date

How much money do you have

hobbies

Go and add get/set methods yourself, as well as construction and toString

Two, Gson

First, we go to the maven warehouse and download the Gson package to him. The download address is: https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.6

After downloading it, we put it in our previous AjaxHelloWorld project

Then, let's write a GsonTest.

First, we first instantiate a collection of Student, and then assign some initial values ​​to this collection, which will be used later

Then let's start the operation

1. Instantiate a simple Json object

Here we need to use the JsonObject object provided by Gson, we use it to create an object, and then call the addProperty method to add key-value pairs to this object

Pay attention here, look at the imported package, we are using import com.google.gson.JsonObject; run it

Of course, here we will try something else

Note that only reference types can be added here, values ​​of basic data types cannot be added! !

As you can see, we added another object with a key of 123, but it did not display it, but replaced it. This is what I said before. The same key cannot exist under the same level.

2. Instantiate a simple Json array

To instantiate the Json array, we are using the JsonArray provided by Gson. To add an object, we need to call the add method, as follows

Then we create two JsonObject objects and store them in this array

Run it and see what is output

As you can see, it is an array object

3. Convert the list collection to Json

Let's write another method to convert the list collection we defined before into our Json. Here we are actually calling the toJson method provided by Gson. Let's take a look

Let's run it and see the effect

There is no problem, we copy the results to the website checked by Json to check

There is no problem

Here I want to say that the toJson method of this Gson object has many overloaded methods, which can convert objects of many data structures into Json objects. You can research and study by yourself (collections can be converted, not to mention single objects. Up)

4. Convert Json to Java object

We convert the Json string into a Java object. Here, we use the Json we just generated to test

Here, we need to use the fromJson method provided by Gson. There are many constructors in the fromJson method.

As you can see, the provided is very complete, we use the penultimate one here, and one more class is needed here.

One more point, here you need to know the format of the specific Json object, otherwise an error will be reported. In general, we all know the format and then convert it

We convert this Json array into a List collection, and then traverse the output

There is no problem. Because we only have one object, we can only traverse one. Of course, the fields here need to correspond, and there is no way to assign these values ​​to the attributes in our objects.

Three, FastJson

In fact, FastJson is very similar to Gson, except that the function names to be called are different, and the parameters are different. The other methods of use are similar. FastJson Maven download link: https://mvnrepository.com/artifact/com.alibaba/fastjson /1.2.75

Download it and throw it into our project

I’m going directly to the code here, so I don’t want to talk so much nonsense.

1. Create a normal Json object

It should be noted here that Gson provides us with JsonObject, while Ali’s FastJson provides us with JSONObject, which is different. See clearly~~

2. Create Json array

3. List collection conversion Json

It should be noted here that after the toJSON method is used for the JSONObject object, it returns an Object object, which is best converted to JSONArray or JSONObject to receive

4. Convert Json string to object

This is because after JSONObject calls parse, it returns an Object object, because I know what type it is, so I use List to receive

 

Generally speaking, both of them have similar functions. In terms of performance, FastJson is better. However, I prefer the conversion of Gson than this kind of strong conversion. Of course, what to use depends on you~

Four, Json and Ajax used together

Here we use Gson to achieve the cooperation of the two~

Let’s modify our previous Servlet first

We use JsonObject to return Json to the front end, the front end is unchanged, we continue to use index2.html to test, start the server, let's see the result

It's OK.

Let's modify it next, let's use the $.get method to get it, let's copy an index3.html

Restart the server, let’s see if it works

it is also fine

Of course, POST is also possible

The reason is that we call doGet here 

 

This is the end of Ajax and Json. I hope everyone can learn and understand them. After all, this stuff needs to be with us for a long time.

 

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/113884703
Recommended