JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,因其簡(jiǎn)單易讀、易于解析而廣泛使用。在Java中處理JSON數(shù)據(jù)可以通過(guò)多種方法,小編將介紹幾種常用的方式,包括使用 org.json 庫(kù)、Gson 庫(kù)和 Jackson 庫(kù)。
1. 使用 org.json 庫(kù)處理JSON數(shù)據(jù)
org.json 是一個(gè)流行的JSON處理庫(kù),它提供了簡(jiǎn)單易用的API來(lái)解析和生成JSON數(shù)據(jù)。
示例:
添加依賴(Maven):
xmlCopy Code<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
解析JSON字符串:
javaCopy Codeimport org.json.JSONObject;
import org.json.JSONArray;
public class JSONExample {
public static void main(String[] args) {
String jsonString = "{ \"name\": \"John\", \"age\": 30, \"city\": \"New York\" }";
// 解析JSON
JSONObject jsonObject = new JSONObject(jsonString);
// 訪問(wèn)數(shù)據(jù)
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);
// 生成JSON
JSONObject newJsonObject = new JSONObject();
newJsonObject.put("name", "Alice");
newJsonObject.put("age", 25);
newJsonObject.put("city", "Los Angeles");
System.out.println("Generated JSON: " + newJsonObject.toString());
}
}
2. 使用 Gson 庫(kù)處理JSON數(shù)據(jù)
Gson 是 Google 提供的一個(gè)開源庫(kù),用于將Java對(duì)象與JSON進(jìn)行相互轉(zhuǎn)換。它支持更復(fù)雜的數(shù)據(jù)結(jié)構(gòu)和自定義序列化/反序列化。
示例:
添加依賴(Maven):
xmlCopy Code<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
解析和生成JSON:
javaCopy Codeimport com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Map;
public class GsonExample {
public static void main(String[] args) {
Gson gson = new Gson();
String jsonString = "{ \"name\": \"John\", \"age\": 30, \"city\": \"New York\" }";
// 解析JSON
Map<String, Object> map = gson.fromJson(jsonString, new TypeToken<Map<String, Object>>() {}.getType());
String name = (String) map.get("name");
double age = (Double) map.get("age");
String city = (String) map.get("city");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
// 生成JSON
Person person = new Person("Alice", 25, "Los Angeles");
String jsonOutput = gson.toJson(person);
System.out.println("Generated JSON: " + jsonOutput);
}
static class Person {
private String name;
private int age;
private String city;
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
}
}
3. 使用 Jackson 庫(kù)處理JSON數(shù)據(jù)
Jackson 是另一個(gè)流行的JSON處理庫(kù),它提供了高效的JSON數(shù)據(jù)綁定功能,支持復(fù)雜的數(shù)據(jù)模型和自定義處理。
示例:
添加依賴(Maven):
xmlCopy Code<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.1</version>
</dependency>
解析和生成JSON:
javaCopy Codeimport com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
public class JacksonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{ \"name\": \"John\", \"age\": 30, \"city\": \"New York\" }";
try {
// 解析JSON
JsonNode jsonNode = mapper.readTree(jsonString);
String name = jsonNode.get("name").asText();
int age = jsonNode.get("age").asInt();
String city = jsonNode.get("city").asText();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
// 生成JSON
Person person = new Person("Alice", 25, "Los Angeles");
String jsonOutput = mapper.writeValueAsString(person);
System.out.println("Generated JSON: " + jsonOutput);
} catch (Exception e) {
e.printStackTrace();
}
}
static class Person {
private String name;
private int age;
private String city;
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
}
在Java中處理JSON數(shù)據(jù),可以選擇不同的庫(kù)來(lái)適應(yīng)不同的需求和場(chǎng)景。org.json 庫(kù)提供了基礎(chǔ)的JSON處理功能,適合簡(jiǎn)單的使用場(chǎng)景;Gson 和 Jackson 提供了更為豐富和復(fù)雜的功能,適合處理復(fù)雜的數(shù)據(jù)結(jié)構(gòu)和高級(jí)的序列化/反序列化需求。根據(jù)項(xiàng)目的需求選擇合適的庫(kù),可以幫助你高效地處理JSON數(shù)據(jù)。