JSON In Java's working principle in the Java class library
JSON (JavaScript Object Notation) is a lightweight data exchange format that is often used in front -end data transmission and storage.In Java, there are various types of libraries that can realize JSON analysis and generation.
1. JSON analysis: The JSON parser in the Java class library can convert the JSON string into a Java object.Below is a simple example code that shows how to use the JSON parser to resolve the JSON string:
import org.json.JSONObject;
public class JsonParsingExample {
public static void main(String[] args) {
// json string
String jsonStr = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
// Use the JSON object to parse the string
JSONObject jsonObj = new JSONObject(jsonStr);
// Get the attribute value in the object
String name = jsonObj.getString("name");
int age = jsonObj.getInt("age");
String city = jsonObj.getString("city");
// Print attribute value
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
After executing the above code, the output will be the following results:
Name: John
Age: 30
City: New York
2. JSON generation: The JSON generator in the Java class library can convert Java objects into JSON string.Below is a simple example code that demonstrates how to use the JSON generator to generate the JSON string:
import org.json.JSONObject;
public class JsonGenerationExample {
public static void main(String[] args) {
// Create an empty JSON object
JSONObject jsonObj = new JSONObject();
// Set the attribute value
jsonObj.put("name", "John");
jsonObj.put("age", 30);
jsonObj.put("city", "New York");
// Convert json objects to string
String jsonStr = jsonObj.toString();
// Print json string
System.out.println(jsonStr);
}
}
After executing the above code, the output will be the following results:
{"name":"John","age":30,"city":"New York"}
3. Related configuration: Before using the JSON class in the Java class library, you need to add the relevant class library to the project of the project.A commonly used JSON processing library is JSON-JAVA (https://github.com/stleary/json-java), which provides a class used to analyze and generate JSON.
To use the JSON-Java class library, you can download it as a jar file and add it to the project's classpath.For example, in the project built by Maven, you can add the following dependencies to the `pom.xml` file:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>
After completing the above configuration, you can use the JSON-JAVA library in the Java code to analyze and generate JSON.
In summary, the JSON working principle in the Java class library is to convert the JSON string into the Java object through the JSON parser, and to convert the Java object into a JSON string through the JSON generator.Before use, the relevant class library needs to be added to the project path to use the class and methods in it.