专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java解析json数组对象

Java解析json数组对象

更新时间:2022-10-17 11:12:10 来源:赢咖4 浏览2434次

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

样本JSON数组{
"books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}

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

JSON-简单的Maven依赖

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

com.googlecode.json-simple
json-simple
1.1.1

将其粘贴在pom.xml文件末尾的 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: [email protected]",
"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 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 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: [email protected]
phone: 9848022338
city: Hyderabad
Area: Madapur
State: Telangana

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>