JSON framework in the Java library use tutorial
JSON (JavaScript Object Notation) is a lightweight data exchange format that stores and transmit data in text formats that are easy to read and write.In Java, we can use various JSON frameworks to process JSON data.This article will introduce how to use the JSON framework in the Java library and provide the corresponding Java code example.
1. The use of the GSON framework
GSON is a Java library provided by Google, which is used to convert Java objects into JSON string and convert the JSON string into a Java object.First, we need to introduce the GSON library in the Java project.You can add the following dependencies in the construction management tools of the project (such as Maven or Gradle):
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
Then, we can use the following code example to convert a Java object to a JSON string:
import com.google.gson.Gson;
public class GsonExample {
public static void main(String[] args) {
// Create a Java object
Person Person = New Person ("Zhang San", 25);
// Create a GSON instance
Gson gson = new Gson();
// Convert java objects to json string
String json = gson.toJson(person);
// Output json string
System.out.println(json);
}
// Define a Person class
static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
}
In the above code, we first created a Person class and instantiated an Person object.Then, we created a GSON instance that converted the Person object into a JSON string by calling the gson.tojson () method.Finally, we output the generated JSON string.
2. Jackson framework use
Jackson is another popular JSON framework that provides many features to process JSON data.To use the Jackson framework, we need to introduce the Jackson-DataBind library in the Java project.In the construction management tool of the project, the following dependencies can be added:
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
Below is a sample code that uses the Jackson framework to convert a Java object to a JSON string:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample {
public static void main(String[] args) throws Exception {
// Create a Java object
Person Person = New Person ("Li Si", 30);
// Create ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();
// Convert java objects to json string
String json = mapper.writeValueAsString(person);
// Output json string
System.out.println(json);
}
// Define a Person class
static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
}
In the above code, we created a Person object and instantiated an ObjectMapper object.Then, we use Mapper.Writevalueasstring () method to convert the Person object to a JSON string and finally output it.
In summary, we introduced how to use the JSON framework in the Java library, and provide a sample code for using the GSON and Jackson framework to convert Java objects into JSON string.You can choose the suitable JSON framework according to your needs to process JSON data.