Today is the 167th day for Liu Xiaoai to learn Java by herself.

Thank you for watching. Thank you.


The study plan is as follows:

  • A utility class, JsonUtils, is used to import data into the index library. Here is a summary.

I learned about JSON a long time ago, and I’ve been using it since then, as a medium for data transfer between the front end and the back end.

To facilitate data transfer, the data stored in the database is sometimes set as a JSON string.

In Java, the representation of data is nothing more than objects, so it involves serialization and deserialization of Json strings.

Serialization of Json

The JsonUtils utility class is quite powerful, with five methods that can do various transformations of JSON strings.

There is a core class, ObjectMapper, on which the following methods are implemented.

Mapper is an object of the ObjectMapper class.


Serialization of JSON

The toString() method converts any object to a JSON-formatted string.

  • The first if condition determines whether the object is empty.
  • The second if condition checks whether the object type is a string,
  • Mapper calls the writeValueAsString() method to convert an object to a JSON string.

Java ordinary objects, list collections, and Map collections can all be converted directly using this method.

Ok, after you’ve written the code, do a test:


Create a User class with name and age attributes. Create two objects for a test.

The toString() method is called to convert the object to a JSON-formatted string, the userOneJosn above.

Similarly, you can convert a List collection to a JSON-formatted string, namely, userListJson, as described above.

The same is true for map collections, which I won’t go into too much detail.

This completes the serialization of JSON.

Json deserialization

There will be serialization and there will be deserialization.


Json deserialization: convert to object

The toBean() method converts a JSON string into an object.

Call the readValue method with mapper with two arguments:

  • Json: this is a JSON string.
  • TClass: that is, the Class object to be converted to.

Json deserialization: convert to List

The toList() method converts the JSON string into a List collection.

Call the readValue method with mapper.

  • Json: Also a JSON string.
  • Mapper.gettypefactory ().constructListType() specifies the generics of the collection.
  • List.class: the class object of the List collection.
  • EClass is the Class object corresponding to the object in the collection to be converted.

Take a test:


Deserialization is like reversing the serialization process.

Both json strings are the result of a serialization test.

  • The toBean() method is called to convert the JSON string into an object.
  • Call toList() to convert the JSON string to a List collection.

Note: If the JSON string is a collection, as in listJson above, calling the toBean() method returns an error.

Json deserialization

In addition to regular objects and List collections, there are Map collections and a generic method.


④ Json deserialization: convert to Map

The toMap() method converts a JSON string into a Map collection.

Call readValue() with mapper, note the difference with toList() :

  • ConstructListType () corresponds to a List collection.

  • ConstructMapType () corresponds to the Map collection.

Class objects corresponding to keys and values must be specified in the Map collection.

⑤ JSON deserialization universal version

For the understanding of this method, all the above ②③④ can be achieved through it.

If you look at the arguments, the first argument is a JSON string, but the second argument is different.

In fact, ②③④ is equivalent to making a vertical segmentation in the method:

  • The second argument to the toBean method in ② directly specifies the Class object of the object.
  • The second argument to the toList method in ③ is specified by the constructListType() method.
  • The toMap method’s second argument in (4) is illustrated by the constructMapType() method.

Take a test:


The toMap() method is called to deserialize mapJson into a map, that is, a key-value pair, where key is 1 and value is a User object.

Call the nativeRead() method to convert the JSON string to the corresponding object.

In this example, we have a complex JSON string, a map, a key string, and a value list containing a User object.

The last

Do not look back on yourself, I am @Liu Xiaoai

I am a post-95 hupiao who works during the day and studies at night. I only want to learn self-discipline and do a good job of myself. I hope my daily punch card can bring you courage.

This article is formatted using MDNICE