Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Java object to Json

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to convert Java objects to Json. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

When it comes to serialization and deserialization of Java objects, the first thing that comes to mind is the Serializable interface of Java, which may be used in DTO objects between two systems for data transfer between systems. Or it may be used in RPC (remote method calls).

But in fact, if used for data transmission, xml and json data formats are used more often. I worked in the first company is also used to encapsulate Java objects and xml conversion tools, you may say why not XStream, XStream is really easy to use, but in the Applet environment can use the xml class library is only jdom, and even dom4j in the Applet environment do not have permission to use (some features of Java reflection require permissions, Applet security mechanisms do not allow).

The following is about the conversion between Java objects and Json. At present, there are three kinds of Json class libraries commonly used in Java, namely, fastjson, jackson and gson, which introduce how to convert a Java object into Json and a Json string into Java object respectively.

First, let's look at Maven dependency.

Com.alibaba fastjson 1.2.12 com.fasterxml.jackson.core jackson-core 2.7.4 com.fasterxml.jackson.core jackson-annotations 2.7.4 com.fasterxml.jackson.core jackson-databind 2.7.4 com.google.code.gson gson 2.6.2

Need serialized POJO and initialization code

The following POJO is used for the following three class libraries

Public class User {public User () {} private String id; private String name; private String password; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password @ Override public String toString () {return "User [id=" + id + ", name=" + name + ", password=" + password + "]";}}

/ * * initialize User object * @ return user * / private static User initUser () {User user = new User (); user.setId ("1"); user.setName ("jison"); user.setPassword ("jison"); return user;}

III. The use of fastjson

The main utility class of fastjson is JSON. The following code implements the serialization and deserialization of Java objects.

/ / serialize Java objects into Json strings String objectToJson = JSON.toJSONString (initUser ()); System.out.println (objectToJson); / / deserialize Json strings into Java objects User user = JSON.parseObject (objectToJson, User.class); System.out.println (user)

IV. The use of jackson

Jackson we often use the ObjectMapper class under its data binding package. The following code implements the serialization and deserialization of Java objects.

ObjectMapper objectMapper = new ObjectMapper (); / / serialize Java objects into Json strings String objectToJson = objectMapper.writeValueAsString (initUser ()); System.out.println (objectToJson); / / deserialize Json strings into Java objects User user = objectMapper.readValue (objectToJson, User.class); System.out.println (user)

5. The use of gson

The main utility class of gson is Gson, which is constructed using GsonBuilder. The following code implements the serialization and deserialization of Java objects

Gson gson = new GsonBuilder () .create (); / serialize the Java object to the Json string String objectToJson = gson.toJson (initUser ()); System.out.println (objectToJson); / / deserialize the Json string to the Java object User user = gson.fromJson (objectToJson, User.class); System.out.println (user)

The complete code of the above three json class libraries is as follows:

Public class JsonUtils {/ * initialize User object * @ return user * / private static User initUser () {User user = new User (); user.setId ("1"); user.setName ("jison"); user.setPassword ("jison"); return user } public static void main (String [] args) throws Exception {/ / fastjson usage fastjson (); / / jackson usage jackson (); / / gson usage gson () } private static void fastjson () {/ / serialize Java objects into Json strings String objectToJson = JSON.toJSONString (initUser ()); System.out.println (objectToJson); / / deserialize Json strings into Java objects User user = JSON.parseObject (objectToJson, User.class) System.out.println (user);} private static void jackson () throws Exception {ObjectMapper objectMapper = new ObjectMapper (); / / serialize the Java object to the Json string String objectToJson = objectMapper.writeValueAsString (initUser ()); System.out.println (objectToJson) / / deserialize Json strings into Java objects User user = objectMapper.readValue (objectToJson, User.class); System.out.println (user);} private static void gson () {Gson gson = new GsonBuilder () .create () / / serialize Java objects into Json strings String objectToJson = gson.toJson (initUser ()); System.out.println (objectToJson); / / deserialize Json strings into Java objects User user = gson.fromJson (objectToJson, User.class); System.out.println (user) }} on how to carry out the conversion between Java objects and Json to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report