How to use the JSON framework in the Java library
How to use the JSON framework in the Java library
JSON (JavaScript Object Notation) is a lightweight data exchange format, which has become one of the data transmission format commonly used in Web development.In Java development, there are many JSON frameworks to choose from, such as Jackson, GSON, and Fastjson.This article will focus on how to use the JSON framework in the Java library and demonstrate its usage through the example code.
1. Use of the Jackson framework
Jackson is a commonly used JSON processing framework that provides a series of APIs for serialization and back -sequential operation between Java objects and JSON.
The following is an example code that uses the JACKSON framework to implement the serialization of the Java object to the JSON string:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// Create a Java object
Person person = new Person("Tom", 25);
// Sequence java objects to JSON string
String json = mapper.writeValueAsString(person);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
// omit the constructive method, Getters and Setters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
In the above example code, we created a Person class and used the ObjectMapper class to sequence the Person object to JSON string.The output of the example is as follows:
json
{"name":"Tom","age":25}
Second, the use of the GSON framework
GSON is a JSON processing framework developed by Google. It also provides a set of APIs for serialization and dependent operation between Java objects and JSON.
The following is an example code that uses the GSON framework to implement the serialization of the Java object to the JSON string:
import com.google.gson.Gson;
public class JsonExample {
public static void main(String[] args) {
Gson gson = new Gson();
// Create a Java object
Person person = new Person("Tom", 25);
// Sequence java objects to JSON string
String json = gson.toJson(person);
System.out.println(json);
}
}
class Person {
private String name;
private int age;
// omit the constructive method, Getters and Setters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
In the above example code, we use the GSON class to sequence the Person object to the JSON string.Example output is the same as the Jackson framework:
json
{"name":"Tom","age":25}
Third, the use of the Fastjson framework
Fastjson is a high -performance JSON processing framework developed by Alibaba. It also provides a series of APIs for serialization and dependent operation between Java objects and JSON.
The following is an example code that uses the Fastjson framework to implement the serialization of the Java object to the JSON string:
import com.alibaba.fastjson.JSON;
public class JsonExample {
public static void main(String[] args) {
// Create a Java object
Person person = new Person("Tom", 25);
// Sequence java objects to JSON string
String json = JSON.toJSONString(person);
System.out.println(json);
}
}
class Person {
private String name;
private int age;
// omit the constructive method, Getters and Setters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
In the above example code, we use the JSON class to sequence the Person object to the JSON string.Example output is the same as the previous two frameworks:
json
{"name":"Tom","age":25}
Summarize:
Whether it is Jackson, GSON or Fastjson, it provides easy -to -use APIs for operating JSON in the Java library.Developers can choose suitable JSON frameworks according to actual needs, and conduct serialization and derivativeization of APIs provided by the framework.The example code provided in this article is only used as an entry reference to help readers use the JSON framework quickly.