JSON P's default provider of the program in the Java library (Guide to use the default provider of JSON P Framework in Java Class Libraares)
JSON P's default provision of procedures in the Java library usage guidelines
JSON P (JSON Processing) is a standard framework for the JSON format on the Java platform.The framework allows developers to easily analyze and generate JSON data in Java applications.JSON P's default program is a built -in implementation of the JSON P framework. This article will introduce how to use this default program in the Java library.
1. Add JSON P dependency
To use the JSON P framework, the default provision of the program, first of all, you need to add JSON P related dependencies to the Java project.You can add the following dependencies in the configuration file of the project construction tool (such as Maven or Gradle): the following dependencies:
Maven:
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
Gradle:
groovy
implementation 'javax.json:javax.json-api:1.1.4'
implementation 'org.glassfish:javax.json:1.1.4'
2. Analyze json data
When using the JSON P default program to analyze JSON data, first of all, a JSONreader object needs to be created.You can create a jsonreader object through the static method of the JSONREDERFACTORY class `creatRereeder ()` `Java.iO.Reader's object as a parameter. The Reader object contains JSON data to be parsed.
The following example demonstrates how to use the JSON P default program to analyze JSON data:
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import java.io.StringReader;
public class JsonParserExample {
public static void main(String[] args) {
String jsonStr = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
// Create a StringReader that contains analysis of JSON data
StringReader reader = new StringReader(jsonStr);
// Create a JSONREADER object
JsonReader jsonReader = Json.createReader(reader);
// Read json data and generate jsonObject
JsonObject jsonObject = jsonReader.readObject();
// Obtain the corresponding value through the key
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
String city = jsonObject.getString("city");
// Print the Resolution Result
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
// Turn off jsonreader
jsonReader.close();
}
}
In the above example, we created a StringReader object that contains JSON data to be parsed.Then, the JSON.CreateReader () method provided by JSON P created a JSONREADER object.Next, by calling the ReadObject () method of JSONREADER, JSON data is parsed as a JSONObject object.Finally, get the corresponding value in JSONObject through the key.
3. Generate JSON data
In addition to analyzing JSON data, JSON P's default program also supports generating JSON data in the Java code.The following is an example that shows how to use JSONWRITER to generate JSON data:
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import java.io.StringWriter;
public class JsonGeneratorExample {
public static void main(String[] args) {
// Create jsonObject objects
JsonObject jsonObject = Json.createObjectBuilder()
.add("name", "John")
.add("age", 30)
.add("city", "New York")
.build();
// Create StringWriter to write the generated JSON data into the string
StringWriter writer = new StringWriter();
// Create JSONWRITER objects
JsonWriter jsonWriter = Json.createWriter(writer);
// Write jsonObject into jsonWriter
jsonWriter.writeObject(jsonObject);
// Printing JSON data
System.out.println(writer.toString());
// Turn off jsonWriter
jsonWriter.close();
}
}
In the above example, we used the `json.createObjectbuilder ()` method to create a JSONObject object, and add key value pairs through the `add ()` method.We then created a Stringwriter object to write the generated JSON data into the string.Then, the JSONWriter object was created using the method of `json.createwriter ()` and wrote JSONObject into it.Finally, by calling the `Tostring ()" method, print the JSON data in StringWriter.
The above is the basic use guide in the Java class library of the JSON P default.Through this simple guide, developers can easily analyze and generate JSON data.I hope this article will be helpful for your learning and development!