[英]java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject error when reading json file iwith java
I have a json file which I'm creating it some vba codes in excel.我有一个 json 文件,我在 excel 中创建了一些 vba 代码。 I want to read this json file with java.我想用java读取这个json文件。 To do this I wrote this codes为此,我编写了此代码
try {
Class.forName("org.json.simple.parser.JSONParser");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONParser parser = new JSONParser();
try {
System.out.println("Reading JSON file from Java program");
FileReader fileReader = new FileReader("C:\\Users\\ftk1187\\Desktop\\jsonExample.json");
JSONArray jsonArray = (JSONArray) parser.parse(fileReader);
JSONObject json=(JSONObject) jsonArray.get(0);
String reference = (String) json.get("reference");
String refType = (String) json.get("refType");
String engType = (String) json.get("engType");
System.out.println("reference: " + reference);
System.out.println("refType: " + refType);
System.out.println("refType: " + refType);
System.out.println("engType: " + engType);
/*JSONArray characters = (JSONArray) json.get("Task");
Iterator i = characters.iterator();
System.out.println("characters: ");
while (i.hasNext()) {
System.out.println(" " + i.next());
}*/
} catch (Exception ex) {
ex.printStackTrace();
}
But the program gives me this error java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject但是程序给了我这个错误java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
My json file like this.我的json文件是这样的。
[
{
"Task": {
"refType": "EM TASK ",
"reference": "72-00-32-020-004",
"engType": "A5",
"DMC": "V2500-A0-72-00-3200-04A-520A-B",
"subTasks": {
"SUBTASK0": "72-00-32-020-065",
"SUBTASK1": "72-00-32-020-066",
"SUBTASK2": "72-00-32-020-067",
"SUBTASK3": "72-00-32-020-068",
"SUBTASK4": "72-00-32-020-069",
"SUBTASK5": "72-00-32-020-070-A00",
"SUBTASK6": "72-00-32-020-070-B00",
"SUBTASK7": "72-0-32-020-070-C00",
"SUBTASK8": "72-00-32-020-070-D00",
"SUBTASK9": "72-00-32-020-070-E00",
"SUBTASK10": "72-00-32-020-070-F00",
"SUBTASK11": "72-00-32-020-070-G00",
"SUBTASK12": "72-00-32-020-071",
"SUBTASK13": "72-00-32-020-072",
"SUBTASK14": "72-00-32-020-058",
"SUBTASK15": "72-00-32-020-073",
"SUBTASK16": "72-00-32-020-074-A00",
"SUBTASK17": "72-00-32-020-074-B00",
"SUBTASK18": "72-00-32-020-075-A00",
"SUBTASK19": "72-00-32-020-075-B00",
"SUBTASK20": "72-00-32-020-323",
"SUBTASK21": "72-00-32-020-324",
"SUBTASK22": "72-00-32-020-076-A00",
"SUBTASK23": "72-00-32-020-076-B00",
"SUBTASK24": "72-00-32-020-077",
"SUBTASK25": "72-00-32-020-078-A00",
"SUBTASK26": "72-00-32-020-078-B00",
"SUBTASK27": "72-00-32-020-079"
}
}
},
Eclipse's console shows this Eclipse 的控制台显示了这一点
Reading JSON file from Java program
reference: null
refType: null
refType: null
engType: null
There is an json array at the top level of your document since:您的文档的顶层有一个 json 数组,因为:
A JSONArray is an ordered sequence of values. JSONArray 是有序的值序列。 Its external text form is a string wrapped in square brackets with commas separating the values.它的外部文本形式是用方括号括起来的字符串,用逗号分隔值。
If your document has only one element, you can simply remove the square brackets.如果您的文档只有一个元素,您只需删除方括号即可。 Otherwise just get JSONObject
from JSONArray
:否则只需从JSONArray
获取JSONObject
:
JSONArray jsonArray = (JSONArray) parser.parse(fileReader);
JSONObject json = (JSONObject) jsonArray.get(0);
JSONObject jsonValue = (JSONObject) json.get("Task");
String reference = (String) jsonValue.get("reference");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.