How to deal with the conversion of Java objects to JSON and its common problems (how to handle the conversion from java objects to json and commit issues)
How to deal with the conversion of Java objects to JSON and its common problems
Introduction:
JSON (JavaScript Object Notation) is a commonly used data exchange format that is widely used in front -end data transmission.In Java development, we often need to convert the Java object to JSON format.This article will introduce how to deal with the conversion of Java objects to JSON and solve some common problems.
1. Use the jackson library:
Jackson is a popular Java library that provides the function of converting Java objects into JSON.The main steps of using Jackson for conversion are as follows:
a) Introduction dependencies:
First of all, you need to introduce Jackson's related dependence in your project.You can add the following dependencies to Maven or Gradle configuration files:
Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.5</version>
</dependency>
Gradle:
groovy
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'
b) Execute conversion:
In the Java code, you can use the ObjectMapper class to execute the Java object to the conversion of JSON.The following is an example:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
try {
// Convert java objects to json string
String json = objectMapper.writeValueAsString(new MyObject());
System.out.println(json);
// Convert json string to Java object
MyObject myObject = objectMapper.readValue(json, MyObject.class);
System.out.println(myObject);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyObject {
private String name;
private int age;
// Eliminate the constructor, Getter, and Setter method
}
In the above examples, we created an ObjectMapper instance, and then converted the Java object to the JSON string with the WriteValueasstring () method.Conversely, we use the readvalue () method to convert the JSON string into a Java object.
2. Treatment of common problems:
a) Circulation reference:
When the Java object exists with mutual reference, it may cause an infinite cycle when converting to JSON.To solve this problem, you can use Jackson's @JSONIDENTITYINFO annotation or @jsonProperty annotation to process cyclic references.
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
class MyObject {
private int id;
private String name;
private MyObject relatedObject;
// Eliminate the constructor, Getter, and Setter method
}
In the above examples, we use @jsonIdityInfo annotations on the MyObject class, specify the generator property to specify a unique identifier through the propriety attribute, and we use ID here.In this way, when there is a cycle reference, Jackson will be processed by ID.
b) Ignore attributes:
Sometimes we want to ignore certain attributes when converting to JSON.You can use Jackson's @JSONIGNORE annotation or set the corresponding attributes when configured ObjectMapper to achieve.
Example of using @jsonignore annotation:
import com.fasterxml.jackson.annotation.JsonIgnore;
class MyObject {
private String name;
@JsonIgnore
private String ignoredField;
// Eliminate the constructor, Getter, and Setter method
}
In the above examples, we use @jsonignore annotations on the iGNOREDFIELD attribute, so that the attribute will be ignored when converting to JSON.
c) Treatment date:
By default, the Date type of Java is expressed in the form of timestamp when converting to JSON.If you want to use other date formats, you can use Jackson's @JSONFORMAT annotation.
import com.fasterxml.jackson.annotation.JsonFormat;
class MyObject {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date date;
// Eliminate the constructor, Getter, and Setter method
}
In the above examples, we use @JSONFORMAT annotations on Date attributes, and set it to shape.string through the shape property. In this way, when converted to JSON, Date will use the specified date format.
in conclusion:
This article introduces the basic steps to convert the Java object to JSON, as well as how to solve some common problems.Using the Jackson library can easily execute the conversion of Java objects to JSON, and support more custom configuration and annotations.I hope this article will help you in dealing with Java objects to JSON's conversion process.