JSON's working principle in the Java class library (Analysis of the Working Principles of JSON in Java in Java Class Libraries)
JSON (JavaScript Object Notation) is a lightweight data exchange format that is widely used in data transmission and storage in modern applications.In Java, many types of libraries can be used to analyze and generate JSON.This article will explore the working principle of JSON in the Java library and provide relevant programming code and configuration examples.
JSON's working principle in the Java library mainly involves two aspects: parsing and generation.Analysis is the process of converting JSON data to the Java object, and the process of generating the Java object into JSON data.
First, let's take a look at the structure of JSON data.JSON consists of a key value pair. The key is a string, and the value can be a string, a number, a Boolean value, an array, an object or a null.In Java, we can use Map to represent JSON objects, where the key is String, and the value can be string, number, Boolean, List, Map or Null.
Next, we introduce two commonly used Java class libraries: GSON and Jackson.GSON is a open source library provided by Google for conversion between Java objects and JSON data.Jackson is another commonly used JSON processing class library and is also an open source project.
For GSON, we first need to add GSON dependencies to the project configuration file.For example, in the Maven project, the following dependencies can be added to the POM.XML file:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
Next, we can use GSON's API to resolve JSON data.The following is an example code:
import com.google.gson.Gson;
public class JsonExample {
public static void main(String[] args) {
String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("City: " + user.getCity());
}
}
class User {
private String name;
private int age;
private String city;
// Getters and setters
}
In this example, we used the GSON's Fromjson method to convert JSON data to User object.We can then obtain the conversion data through the GETTER method of the User object.
For generating JSON data, we can use the GSON Tojson method.The following is an example code:
import com.google.gson.Gson;
public class JsonExample {
public static void main(String[] args) {
User user = new User("John", 30, "New York");
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json);
}
}
class User {
private String name;
private int age;
private String city;
// Constructors, getters and setters
}
In this example, we created a User object and converted it to JSON data with the Tojson method of GSON.Then we can print JSON data.
Now let's take a look at the usage of the Jackson class library.Similar to Gson, we need to add Jackson dependencies to the project configuration file.The following is an example of the configuration of the Maven project:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
For Jackson, we can use the ObjectMapper class to analyze and generate JSON data.The following is an example code:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonExample {
public static void main(String[] args) throws Exception {
String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("City: " + user.getCity());
String jsonString = objectMapper.writeValueAsString(user);
System.out.println(jsonString);
}
}
class User {
private String name;
private int age;
private String city;
// Getters and setters
}
In this example, we use ObjectMapper's ReadValue method to convert JSON data into User objects, and use WritevalueASSTRING method to convert the USER object to JSON data.
In summary, the working principle of JSON in the Java library mainly involves two processes of analysis and generating.By using the API provided by the class library (such as GSON and Jackson), we can easily convert between Java objects and JSON data.By understanding and using these class libraries, we can better process the transmission and storage of JSON data in Java applications.