Learn to use the JSON conversion framework in the Java library
Learn to use the JSON conversion framework in the Java library
Introduction:
In Java development, JSON conversion is a common and important task.JSON (JavaScript Object Notation) is a lightweight data format for data exchange, which is easy to read and ease.In order to make JSON codes and decoding in Java, we can use various types of libraries and frameworks to simplify this process.This article will introduce the JSON conversion framework in several commonly used Java libraries, as well as their usage methods and example code.
1. Gson:
GSON is a popular JSON conversion framework provided by Google. It can convert the Java object into a JSON format string, or it can also convert the JSON string into a Java object.The following is an example code using GSON for JSON conversion:
// Import the necessary library
import com.google.gson.Gson;
public class GsonExample {
public static void main(String[] args) {
// Create GSON objects
Gson gson = new Gson();
// Convert java objects to json string
MyObject obj = new MyObject("John", 25);
String jsonStr = gson.toJson(obj);
System.out.println(jsonStr);
// Convert json string to Java object
MyObject newObj = gson.fromJson(jsonStr, MyObject.class);
System.out.println(newObj.getName());
System.out.println(newObj.getAge());
}
}
// Custom a Java object
class MyObject {
private String name;
private int age;
// omit the creation function and getter/setter method
MyObject(String name, int age) {
this.name = name;
this.age = age;
}
}
2. Jackson:
Jackson is another commonly used JSON conversion framework that supports mutual conversion between Java and JSON string.The following is an example code using Jackson for JSON conversion:
// Import the necessary library
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample {
public static void main(String[] args) throws Exception {
// Create ObjectMapper objects
ObjectMapper mapper = new ObjectMapper();
// Convert java objects to json string
MyObject obj = new MyObject("John", 25);
String jsonStr = mapper.writeValueAsString(obj);
System.out.println(jsonStr);
// Convert json string to Java object
MyObject newObj = mapper.readValue(jsonStr, MyObject.class);
System.out.println(newObj.getName());
System.out.println(newObj.getAge());
}
}
// Custom a Java object
class MyObject {
private String name;
private int age;
// omit the creation function and getter/setter method
MyObject(String name, int age) {
this.name = name;
this.age = age;
}
}
3. Fastjson:
Fastjson is a fast and powerful JSON conversion framework, developed by Alibaba Group.It supports rapid conversion between Java objects and JSON string, which has high performance.The following is a sample code using Fastjson for JSON conversion:
// Import the necessary library
import com.alibaba.fastjson.JSON;
public class FastjsonExample {
public static void main(String[] args) {
// Convert java objects to json string
MyObject obj = new MyObject("John", 25);
String jsonStr = JSON.toJSONString(obj);
System.out.println(jsonStr);
// Convert json string to Java object
MyObject newObj = JSON.parseObject(jsonStr, MyObject.class);
System.out.println(newObj.getName());
System.out.println(newObj.getAge());
}
}
// Custom a Java object
class MyObject {
private String name;
private int age;
// omit the creation function and getter/setter method
MyObject(String name, int age) {
this.name = name;
this.age = age;
}
}
Summarize:
This article introduces the JSON conversion framework in several commonly used Java libraries, including GSON, Jackson and Fastjson.Through these frameworks, we can easily convert between Java and JSON string to achieve data encoding and decoding.According to different needs and preferences, choosing the JSON conversion framework that suits you can improve work efficiency and code quality.I hope this article can help you learn and use the JSON conversion framework in the Java class library.