The use of the use of the JSON Library framework in the Java class library
JSON (JavaScript Object Notation) is a lightweight format for data exchange and storage.It represents data in a simple and easy to read, and is widely used in modern programming languages.In the Java library, there are many different JSON libraries to choose from in order to process and operate JSON data.
This article will introduce some commonly used Java JSON libraries and provide guidelines and related example code.
1. JSONObject and JSONARAY:
The JSON library usually offers a JSONObject class and a JSONARRAY class to represent objects and arrays in JSON.The JSONObject class provides a method of operating and accessing JSON object attributes, while the JSONARAY class is used to operate and access the JSON array.
Below is an example code using JSONObject and JSONARRAY class:
import org.json.JSONArray;
import org.json.JSONObject;
public class JsonExample {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put ("name", "Zhang San");
jsonObject.put("age", 25);
jsonObject.put ("City", "Beijing");
JSONArray jsonArray = new JSONArray();
jsonArray.put("Java");
jsonArray.put("Python");
jsonArray.put("C++");
jsonObject.put("skills", jsonArray);
String jsonStr = jsonObject.toString();
System.out.println(jsonStr);
}
}
In the above code, we first created a JSONObject object and added some attributes using the PUT method.Then, we created a JSONARAY object and added some elements to it.Finally, we added the JSONARAY object to a attribute of the JSONObject object and converted the entire JSONObject object into a JSON string.
2. GSON library:
GSON is a powerful JSON library developed by Google. It provides a flexible API for converting Java objects into JSON string and converting JSON string into Java objects.
The following is a sample code that uses the GSON library to serve the object serialization and derivativeization:
import com.google.gson.Gson;
public class GsonExample {
public static void main(String[] args) {
// java objects converted to json string
Person Person = New Person ("Li Si", 30, "Shanghai");
Gson gson = new Gson();
String jsonStr = gson.toJson(person);
System.out.println(jsonStr);
// json string convert to Java object
String json = "{\" name \ ": \" Wang Wu \ ", \" Age \ ": 28," City \ ": \" Guangzhou \ "}";
Person personObj = gson.fromJson(json, Person.class);
System.out.println(personObj.getName());
}
static class Person {
private String name;
private int age;
private String city;
// getters and setters
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
}
}
In the above code, we created a Java class called Person, and used GSON to serialize it into a JSON string, and then turn the JSON string back -to -order into the Java object.
3. Jackson library:
Jackson is another very popular JSON library.It provides more advanced features, such as supporting custom conversion between JSON and Java objects, and supporting formats such as XML and YAML.
The following is an example code that uses the Jackson library for object serialization and derivativeization:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample {
public static void main(String[] args) throws Exception {
// java objects converted to json string
Person Person = New Person ("Zhao Liu", 35, "Shenzhen");
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = objectMapper.writeValueAsString(person);
System.out.println(jsonStr);
// json string convert to Java object
String json = "{\" name \ ": \" Qian 7 \ ", \" Age \ ": 40," City \ ": \" Chengdu \ "}";
Person personObj = objectMapper.readValue(json, Person.class);
System.out.println(personObj.getName());
}
static class Person {
private String name;
private int age;
private String city;
// getters and setters
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
}
}
In the above code, we use the ObjectMapper class of the Jackson library to convert the Person object into a JSON string, and use the same class to convert the JSON string to Person object.
Summarize:
This article introduces some guidelines and example code in the JSON library in the Java library.The JSONObject and the JSONARAY class provides basic JSON operation methods, while the GSON and Jackson libraries provide more advanced functions and more convenient APIs.Select the library that suits your needs and use them according to the example code, you can easily process and operate JSON data.