JSON uses JSON in Java for object serialization and dependentization
JSON (JavaScript Object Notation) is a lightweight data exchange format, which is often used to represent structured data.In Java, JSON can be used for serialization and derivativeization of the object, converting the object as a string in JSON format for storage or transmission, or the derivative of the string in the JSON format.
JSON serialization and derivativeization in Java can use third -party libraries, such as Jackson, GSON, or Fastjson.These libraries provide convenient API and tools to make the operation of JSON data simple and easy to use.
1. JSON serialization
JSON serialization refers to a string that converts the Java object into a JSON format.The following is an example code that uses the Jackson library for JSON serialization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class SerializationExample {
public static void main(String[] args) {
// Create an object to serialize
Person Person = New Person ("Zhang San", 18, "Male");
try {
// Create ObjectMapper objects
ObjectMapper objectMapper = new ObjectMapper();
// Turn the object serial to JSON string
String json = objectMapper.writeValueAsString(person);
// Print output json string
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
private String gender;
// Construction method, Getter and Setter omit
// Use the jackson library to provide a non -parameter construction method
public Person() {}
}
In the above code, we created a Person class containing the name, Age, and Gender attributes.Using the ObjectMapper class of Jackson, we can sequence the Person object into a JSON string.By calling the Writevalueasstring () method, the object to be serialized can be transformed into a JSON string.Finally, we print the serialized json string.
Second, JSON's counter -serialization
JSON's back -sequentialization refers to converting the string of the JSON format into a Java object.The following is an example code that uses the Jackson library for JSON derivatives:
import com.fasterxml.jackson.databind.ObjectMapper;
public class DeserializationExample {
public static void main(String[] args) {
// To be a back -sequential JSON string
String json = "{\" name \ ": \" Zhang San \ ", \" Age \ ": 18," gender \ ": \" male \ "}";
try {
// Create ObjectMapper objects
ObjectMapper objectMapper = new ObjectMapper();
// Turn the JSON string back -sequence to Person object
Person person = objectMapper.readValue(json, Person.class);
// Print the object of the output back -sequentialization
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getGender());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
private String gender;
// Construction method, Getter and Setter omit
// Use the jackson library to provide a non -parameter construction method
public Person() {}
}
In the above code, we created a JSON string to indicate the attribute of a Person object.Using the ObjectMapper class of Jackson, we can turn the JSON string back -sequencing into a Person object.By calling the Readvalue () method, the type of JSON string and target objects can be transformed into the Java object.Finally, we can print the attributes of the Person object after the output of the output.
Summary: The above is an example code that uses the Jackson library for JSON serialization and dependentization.Of course, in addition to Jackson, you can also use other JSON libraries, such as GSON and Fastjson, which are similar to their usage, but API is slightly different.Using JSON for object serialization and desertification can easily process structured data in the Java program.