Introduction to JSON in Java's technical principles in Java Library (An INTRODUCTION to the Technical Principles of Java in Java Class Libraries)
Introduction to JSON in Java in Java Library
JSON (JavaScript Object Notation) is a lightweight data exchange format that is often used in data transmission and storage in network communication.In the Java class library, there are many tools and libraries that can be used to process JSON data.This article will introduce the principle of using JSON technology in the Java library.
1. JSON basic concept
JSON is a text format that uses simple syntax to represent the data structure.It consists of a key value pair. The key value is separated between the comma, and the colon separates between the key and the values.Data can be numbers, string, Boolean value, array, object and other types.In Java, JSON data is usually treated as string processing.The following is a JSON example:
{
"name": "Zhang San",
"age": 20,
"isStudent": true,
"Hobbies": ["Basketball", "Football", "Music"],
"address": {
"City": "Beijing",
"Country": "China"
}
}
2. The conversion of JSON and Java objects
Using the JSON technology in the Java class library, you can convert the Java object to JSON data, or you can convert JSON data to Java objects.This process is called serialization and derivativeization.
Serialization converts the Java object to JSON data, and can use the JSON class or object mapping tools in the class library, such as Jackson and GSON.These tools provide many methods and annotations to control the details in the process of serialization, such as the exclusion of fields and the formatting of the date.
Code example:
// Import related class libraries
import com.fasterxml.jackson.databind.ObjectMapper;
// Create a Java object
Person Person = New Person ("Zhang San", 20, TRUE);
// Create ObjectMapper objects
ObjectMapper objectMapper = new ObjectMapper();
// Sequence java objects to JSON string
String jsonString = objectMapper.writeValueAsString(person);
The deepery sequentialization converts JSON data to the Java object, and it can also use the JSON class or object mapping tool.The tool can automatically map to the specific Java object type according to the structure of the JSON data.
Code example:
// Turn the JSON string back -sequencing into the Java object
Person person = objectMapper.readValue(jsonString, Person.class);
3. The conversion of JSON and Java collection
In addition to the Java object, JSON data can also be converted with Java (such as list, map, etc.).The class library provides corresponding methods to handle these conversion.
Code example:
// Convert the list object to the JSON array
List<Person> personList = new ArrayList<>();
PersonList.add (New Person ("Zhang San", 20, TRUE);
PersonList.add (new person ("Li Si", 25, FALSE);
String jsonArray = objectMapper.writeValueAsString(personList);
// Sequence the JSON array back to List object
List<Person> personList = objectMapper.readValue(jsonArray, new TypeReference<List<Person>>() {});
4. JSON implementation principle
The JSON technology in the Java class library converts the data into the corresponding Java object or collection by analyzing the syntax structure of JSON data.This process involves technologies such as string processing, reflection, structural analysis.At the bottom, it uses string analysis, regular expression matching, traditional parser and other methods.
5. Related configuration
When using JSON technology in the Java library, you may need to add the corresponding dependency library to the project.For example, when using the Jackson library, you need to add the following configuration in the project construction file (such as Maven or Gradle):
Maven configuration:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
</dependency>
Gradle configuration:
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.2'
In addition, some serialized and dependentization options can be configured in the code.For example, when using the Jackson library, you can use the annotation to control the serialization and deepening behavior of the field.
Summarize:
This article introduces the principles of JSON technology in the Java class library, including the basic concepts of JSON, the conversion of Java objects and JSON data, the conversion of JSON and Java collection, and the implementation of JSON.Using JSON technology in the Java class library can easily process JSON data, playing an important role in network communication and data storage.