【发布时间】:2017-06-02 17:22:49
【问题描述】:
我正在尝试解析一个子文件,但我不知道自己做错了什么(当然,我也不知道自己做对了什么)。
文件.json
[{
"arrOne":{
"one":"a",
"two":"b",
"three":"c",
"four":"d",
"five":"e"
},
"elemTwo":"f",
"elemThree":"g",
"elemFour":"h",
"elemFive":"i",
"arrSix":[{
"six":1,
"seven":2,
"eight":"j"
}]}]
代码:
import java.io.FileNotFoundException;
import java.io.FileReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
//...........
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("/path/to/file.json"));
JSONObject json = (JSONObject) obj;
String unit = (String) json.get("elemTwo");
System.out.println(unit);
我收到错误 ClassCastException:org.json.simple.JSONArray 无法转换为 org.json.simple.JSONObject。说实话,我不知道我在做什么。任何帮助都会很棒!谢谢!
【问题讨论】:
-
您的数据中有一个 JSON 数组(它包含在
[]中)。所以,如果你解析它,它的类型是JSONArray,而不是JSONObject- 正是异常所说的。