如何使用Java讀取/解析JSON陣列?
一個 JSON陣列 是一個有序的值集合,用方括號括起來,即以“[”開頭,以“]”結尾。陣列中的值用“,”(逗號)分隔。
JSON陣列示例
{
"books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}該 json-simple 是一個輕量級的庫,用於處理 JSON物件。使用它,您可以使用Java程式讀取或寫入 JSON文件 的內容。
JSON-Simple Maven依賴
以下是JSON-simple庫的Maven依賴項:
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> </dependencies>
將此貼上到pom.xml檔案的`
示例
首先,讓我們建立一個名為**sample.json**的**JSON**文件,其中包含6個鍵值對和一個數組,如下所示:
{
"ID": "1",
"First_Name": "Krishna Kasyap",
"Last_Name": "Bhagavatula",
"Date_Of_Birth": "1989-09-26",
"Place_Of_Birth":"Vishakhapatnam",
"Salary": "25000"
"contact": [
"e-mail: krishna_kasyap@gmail.com",
"phone: 9848022338",
"city: Hyderabad",
"Area: Madapur",
"State: Telangana"
]
}要使用Java程式從JSON檔案讀取陣列:
- 例項化json-simple庫的JSONParser類。
JSONParser jsonParser = new JSONParser();
- 使用**parse()**方法解析獲得的物件的內容。
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));- 使用**get()**方法檢索與鍵關聯的值。
String value = (String) jsonObject.get("key_name");- 就像其他元素一樣,使用**get()**方法將JSON陣列檢索到JSONArray物件中。
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");- JSONArray類的**iterator()**方法返回一個Iterator物件,可以使用它來迭代當前陣列的內容。
//Iterating the contents of the array
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}下面的Java程式解析上面建立的sample.json檔案,讀取其內容並顯示它們。
示例
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingArrayFromJSON {
public static void main(String args[]) {
//Creating a JSONParser object
JSONParser jsonParser = new JSONParser();
try {
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/test.json"));
//Forming URL
System.out.println("Contents of the JSON are: ");
System.out.println("ID: "+jsonObject.get("ID"));
System.out.println("First name: "+jsonObject.get("First_Name"));
System.out.println("Last name: "+jsonObject.get("Last_Name"));
System.out.println("Date of birth: "+ jsonObject.get("Date_Of_Birth"));
System.out.println("Place of birth: "+ jsonObject.get("Place_Of_Birth"));
System.out.println("Salary: "+jsonObject.get("Salary"));
//Retrieving the array
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
System.out.println("");
System.out.println("Contact details: ");
//Iterating the contents of the array
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}輸出
Contents of the JSON are: ID: 1 First name: Krishna Kasyap Last name: Bhagavatula Date of birth: 1989-09-26 Place of birth: Vishakhapatnam Salary: 25000 Contact details: e-mail: krishna_kasyap@gmail.com phone: 9848022338 city: Hyderabad Area: Madapur State: Telangana
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP