如何使用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