我有一个 json 文件,我正在 excel 中创建一些 vba 代码。我想用java读取这个json文件。为此,我编写了以下代码
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();
}
但是程序给了我这个错误 java.lang.ClassCastException:org.json.simple.JSONArray 无法转换为 org.json.simple.JSONObject
我的 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 的控制台显示了这一点
Reading JSON file from Java program
reference: null
refType: null
refType: null
engType: null
最佳答案
文档的顶层有一个 json 数组,因为:
A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values.
如果您的文档只有一个元素,您只需删除方括号即可。否则只需从 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");
关于使用java读取json文件时出现java.lang.ClassCastException : org. json.simple.JSONArray无法转换为org.json.simple.JSONObject错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751059/