Json数组是括在方括号内的值的有序集合,即,它以'['开始,以']'结尾。数组中的值以','(逗号)分隔。
{
"books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}json-simple是一个轻量级的库,用于处理JSON对象。使用此程序,您可以使用Java程序读取或写入JSON文档的内容。
以下是JSON简单库的maven依赖关系-
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> </dependencies>
将其粘贴在pom.xml文件末尾的<dependencies> </ dependencies>标记中。(在</ project>标记之前)
首先,让我们创建一个名称为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-简单库的JSONParser类。
JSONParser jsonParser = new JSONParser();
使用parse()方法解析 获得的对象的内容。
//解析JSON文件的内容
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对象,您可以使用该对象迭代当前数组的内容。
//迭代数组的内容
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[]) {
//创建一个JSONParser对象
JSONParser jsonParser = new JSONParser();
try {
//解析JSON文件的内容
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/test.json"));
//形成网址
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"));
//检索数组
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
System.out.println("");
System.out.println("Contact details: ");
//迭代数组的内容
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