JSON P's default provider introduction and use examples in the Java Class Library
The JSON-P (JSON Processing) default provider of the JSON-P (JSON Processing) in the Java class library is a standard API used to process JSON data.It provides a rich set of functions that allow developers to easily analyze, generate and operate JSON data.This article will introduce the basic concepts and use examples of the JSON-P default provision of program, and provide you with relevant configuration and complete programming code description.
JSON-P's default program is part of Java Ee 7, which is also included in Java SE 8 from the Java Ee 8.It provides two core categories: JSONObject and JSONARRAY, which are used to represent JSON objects and JSON arrays.
To use JSON-P to provide programs by default, you need to introduce related dependencies in the project.The following is an example based on Maven configuration:
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
Next, let's understand the use of JSON-P default through some simple examples.
#### Example 1: Analyze json data
Suppose we have the following JSON data:
json
{
"name": "Zhang San",
"age": 25,
"City": "Beijing"
}
We can use the JSON-P default program to analyze it as JSONObject and access the attributes.The following is an analysis example:
import javax.json.Json;
import javax.json.JsonObject;
public class JsonParserExample {
public static void main(String[] args) {
String jsondata = "{\" name \ ": \" Zhang San \ ", \" Age \ ": 25," City \ ": \" Beijing \ "}";
JsonObject jsonObject = Json.createReader(new StringReader(jsonData)).readObject();
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
String city = jsonObject.getString("city");
System.out.println ("Name:" + Name);
System.out.println ("age:" + Age);
System.out.println ("City:" + City);
}
}
In the above example, we use the Createreader () method of the JSON class to create a JSONREADER object and read JSON data from the string with the string.Then, we can get the corresponding value from the JSONObject through the getString () and Getint () methods.
#### Example 2: Generate json data
In addition to analyzing JSON data, the JSON-P default program can also be used to generate JSON data.The following is an example of generation:
import javax.json.Json;
import javax.json.JsonObjectBuilder;
public class JsonSerializerExample {
public static void main(String[] args) {
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
jsonObjectBuilder.add ("name", "Li Si");
jsonObjectBuilder.add("age", 30);
jsonObjectBuilder.add ("City", "Shanghai");
JsonObject jsonObject = jsonObjectBuilder.build();
System.out.println(jsonObject.toString());
}
}
In the above example, we use the JSONObjectBuilder class to create a new JSONObjectBuilder object, and then use the ADD () method to add attributes and values.Finally, we generate JSONObject through the BUILD () method, and use the Tostring () method to output it as a JSON string.
Through the above examples, you can understand how to analyze and generate JSON data using the JSON-P default program.Keep in mind that when using JSON-P, you can also perform other operations related to JSON, such as deleting attributes, updating attribute values, etc.
I hope this article will help you understand the default program and its usage of JSON-P in the Java library and its usage.If you have any questions, please ask at any time.