How to process JSON codes and decodes in java (how

How to process JSON coding and decoding in java Introduction: JSON is a lightweight data format for data exchange. It is very common to process JSON codes and decoding in Java.This article will introduce how to use some commonly used libraries in Java to process JSON's coding and decoding operations, and provide corresponding code examples. The JSON coding and decoding of the GSON library: GSON is an open source Java library that converts Java objects and JSON data.It provides a simple and easy -to -use API that can encode an object into a JSON string, or decodes the JSON string as a Java object. First, we need to import the GSON library in the Java project.You can use maven to add dependencies: <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.7</version> </dependency> Below is a sample code that uses GSON for JSON coding and decoding: import com.google.gson.Gson; public class JsonExample { public static void main(String[] args) { // Create an GSON instance Gson gson = new Gson(); // Code the object as a JSON string String json = gson.toJson(new Person("John Doe", 30)); System.out.println ("JSON encoding result:" + json); // Decoding the json string as the object Person person = gson.fromJson(json, Person.class); System.out.println ("JSON decoding result:" + Person.getName () + " -" + Person.getage ()); } // 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; } public String getName() { return name; } public int getAge() { return age; } } } Example of output results: Json coding result: {"name": "John Doe", "Age": 30} JSON decoding result: John Doe -30 JSON coding and decoding of the jackson library: Jackson is another popular Java library for processing JSON data.It provides a complete set of JSON processing functions that can be easily coded and decoded. To use the Jackson library, we need to add corresponding dependencies to the Java project.You can use Maven to add the following dependencies: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.2</version> </dependency> Here are a sample code that uses jackson for JSON codes and decoding: import com.fasterxml.jackson.databind.ObjectMapper; public class JsonExample { public static void main(String[] args) throws Exception { // Create an ObjectMapper instance ObjectMapper mapper = new ObjectMapper(); // Code the object as a JSON string String json = mapper.writeValueAsString(new Person("John Doe", 30)); System.out.println ("JSON encoding result:" + json); // Decoding the json string as the object Person person = mapper.readValue(json, Person.class); System.out.println ("JSON decoding result:" + Person.getName () + " -" + Person.getage ()); } // 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; } public String getName() { return name; } public int getAge() { return age; } } } Example of output results: Json coding result: {"name": "John Doe", "Age": 30} JSON decoding result: John Doe -30 Summarize: This article introduces how to process JSON coding and decoding operations in Java.We understand the coding and decoding of JSON data using the two commonly used libraries of GSON and Jackson, and provide corresponding code examples.Through these libraries, we can easily convert the Java object into a JSON string, or convert the JSON string into a Java object.I hope this article will help you when processing JSON data.