使用java解析和排序JSON数据
我希望使用org.json.simple库解析以下数组,但遇到了一些困难。有人能看看我的json文件和代码,告诉我哪里做错了吗使用java解析和排序JSON数据,java,json,sorting,Java,Json,Sorting,我希望使用org.json.simple库解析以下数组,但遇到了一些困难。有人能看看我的json文件和代码,告诉我哪里做错了吗 { "Company": [ { "Department": "Engineering", "Employee": [ { "EmpName": "Jack", "EmpCode": "8", "Type": "Permanent" }, { "EmpName": "John", "
{
"Company": [
{
"Department": "Engineering",
"Employee": [
{
"EmpName": "Jack",
"EmpCode": "8",
"Type": "Permanent"
}, {
"EmpName": "John",
"EmpCode": "45",
"Type": "Permanent"
}, {
"EmpName": "Ron",
"EmpCode": "9",
"Type": "Contract"
}, {
"EmpName": "Jin",
"EmpCode": "6",
"Type": "Permanent"
}, {
"EmpName": "Jill",
"EmpCode": "",
"Type": "Retired"
}, {
"EmpName": "Sam",
"EmpCode": "89",
"Type": "Permanent"
}, {
"EmpName": "Jonathan",
"EmpCode": "66",
"Type": "Permanent"
}, {
"EmpName": "Craig",
"EmpCode": "",
"Type": "Ex-Employee"
}, {
"EmpName": "Son Hui",
"EmpCode": "4",
"Type": "Permanent"
}, {
"EmpName": "Joshua",
"EmpCode": "12",
"Type": "Contract"
}, {
"EmpName": "Tulip",
"EmpCode": "70",
"Type": "Contract"
}
]
}, {
"Department": "IT",
"Employee": [
{
"EmpName": "Nico",
"EmpCode": "50",
"Type": "Resigned"
}, {
"EmpName": "Phil",
"EmpCode": "103",
"Type": "Resigned"
}
]
}
]
}
请注意,一些EmpCode字段也是空白的。我该如何处理它们。此外,我需要能够在显示数据时重新排列数据,我的意思是我需要根据EmpCode或类型对数据进行排序
我为解析上述json而编写的代码如下所述:
package amazontesting;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
public class json {
private static final String filePath = /emp.json";
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(filePath));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("Department");
//String suites = (String) jsonObject.get("Company");
JSONArray slideContent = (JSONArray) jsonObject.get("Department");
Iterator i = slideContent.iterator();
while (i.hasNext()) {
System.out.println(i.next());
String title = (String) jsonObject.get("Employee");
System.out.println(title);
}
}
catch (FileNotFoundException e)
{}
catch(IOException e)
{}
catch(ParseException e)
{}
}
}
看起来您对Json结构的工作原理以及它可能包含的对象类型有误解 例如,您认为您在这段代码中做了什么
JSONArray slideContent = (JSONArray) jsonObject.get("Department");
Iterator i = slideContent.iterator();
看起来您试图获取名为“Department”的json对象,但它不是JsonArray,而是字符串
"Department": "Engineering"
//Writing code "jsonObject.get("Department")"
//you will get string "Engineering"
//this code will be correct
(JSONArray) jsonObject.get("Employee")
//because a json value for name "Employee" it's an JsonArray
“告诉我做错了什么”不是一个合适的问题。请准确描述您遇到了什么问题,您做了什么来诊断问题,然后构造一个。此外,代码中引用的JSON听起来不像发布的JSON。@OliverCharlesworth抱歉,关于错误的JSON引用,我现在已经更新了。我是处理JSON的新手,所以我在互联网上阅读并尝试使用示例代码。有些帖子提到我应该创建一个基于json的类,然后在主类中调用它,而有些帖子提到上面的代码就可以做到这一点。我尝试运行此代码,department打印空。我假设这会创建一个包含department下所有员工的数组。它将为名称“department”获取值,该值为字符串“Engineering”,感谢您提供的信息。我想我会多读一点这方面的内容