如何使用Java读取/解析JSON数组?

How to read/parse JSON array using Java?

Json数组是括在方括号内的值的有序集合,即,它以'['开始,以']'结尾。数组中的值以','(逗号)分隔。

样本JSON数组

1
2
3
{
"books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}

json-simple是一个轻量级的库,用于处理JSON对象。使用此程序,您可以使用Java程序读取或写入JSON文档的内容。

JSON-简单的Maven依赖

以下是JSON简单库的maven依赖关系-

1
2
3
4
5
6
7
<dependencies>
 <dependency>
   <groupId>com.googlecode.json-simple</groupId>
   json-simple</artifactId>
   <version>1.1.1</version>
 </dependency>
</dependencies>

将其粘贴在pom.xml文件末尾的 标记中。 (在标记之前)

首先,让我们创建一个名称为sample.json的JSON文档,其中包含6个键值对和一个数组,如下所示-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"ID":"1",
"First_Name":"Krishna Kasyap",
"Last_Name":"Bhagavatula",
"Date_Of_Birth":"1989-09-26",
"Place_Of_Birth":"Vishakhapatnam",
"Salary":"25000"
"contact": [
  "e-mail: [email protected]",
  "phone: 9848022338",
  "city: Hyderabad",
  "Area: Madapur",
  "State: Telangana"
 ]
}

要使用Java程序从JSON文件读取数组-

  • 实例化json-简单库的JSONParser类。
1
JSONParser jsonParser = new JSONParser();
  • 使用parse()方法解析获得的对象的内容。
1
2
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));
  • 使用get()方法检索与键关联的值。
1
String value = (String) jsonObject.get("key_name");
  • 就像其他元素一样,使用get()方法将JSON数组检索到JSONArray对象中。
1
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
  • JSONArray类的iterator()方法返回一个Iterator对象,您可以使用该对象迭代当前数组的内容。
1
2
3
4
5
//Iterating the contents of the array
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
 System.out.println(iterator.next());
}

以下Java程序解析上面创建的sample.json文件,读取其内容并显示它们。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
   }
 }
}

输出量

1
2
3
4
5
6
7
8
9
10
11
12
13
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: [email protected]
phone: 9848022338
city: Hyderabad
Area: Madapur
State: Telangana