更新時(shí)間:2022-10-17 11:12:10 來源:動(dòng)力節(jié)點(diǎn) 瀏覽3205次
Json數(shù)組是括在方括號內(nèi)的值的有序集合,即,它以'['開始,以']'結(jié)尾。數(shù)組中的值以','(逗號)分隔。
樣本JSON數(shù)組{
"books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}
json-simple是一個(gè)輕量級的庫,用于處理JSON對象。使用此程序,您可以使用Java程序讀取或?qū)懭隞SON文檔的內(nèi)容。
JSON-簡單的Maven依賴
以下是JSON簡單庫的maven依賴關(guān)系
com.googlecode.json-simple
json-simple
1.1.1
將其粘貼在pom.xml文件末尾的 dependencies>標(biāo)記中。(在 project>標(biāo)記之前)
示例
首先,讓我們創(chuàng)建一個(gè)名稱為sample.json的JSON文檔,其中包含6個(gè)鍵值對和一個(gè)數(shù)組,如下所示:
{
"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文件讀取數(shù)組-實(shí)例化json-簡單庫的JSONParser類。JSONParser jsonParser = new JSONParser();使用parse()方法解析獲得的對象的內(nèi)容。
解析JSON文件的內(nèi)容
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));使用get()方法檢索與鍵關(guān)聯(lián)的值。String value = (String) jsonObject.get("key_name");就像其他元素一樣,使用get()方法將JSON數(shù)組檢索到JSONArray對象中。JSONArray jsonArray = (JSONArray) jsonObject.get("contact");JSONArray類的iterator()方法返回一個(gè)Iterator對象,您可以使用該對象迭代當(dāng)前數(shù)組的內(nèi)容。
迭代數(shù)組的內(nèi)容
Iterator iterator = jsonArray.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
以下Java程序解析上面創(chuàng)建的sample.json文件,讀取其內(nèi)容并顯示它們。
示例
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[]) {
創(chuàng)建一個(gè)JSONParser對象
JSONParser jsonParser = new JSONParser();
try {
解析JSON文件的內(nèi)容
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/test.json"));
形成網(wǎng)址
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"));
檢索數(shù)組
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
System.out.println("");
System.out.println("Contact details: ");
迭代數(shù)組的內(nèi)容
Iterator 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();
}
}
}
輸出結(jié)果
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
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743