ClassCastException:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject [英] ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

查看:70
本文介绍了ClassCastException:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析子文件,但我不知道我在做什么错(当然,我也不真正知道我在做错什么).

I am trying to parse a son file, and I don't know what I'm doing wrong (of course, I don't really know what I'm doing right, either).

file.json

file.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.说实话,我不知道我在做什么.任何帮助将是巨大的!谢谢!

I get the error ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject. Truthfully, I have no idea what I'm doing. Any help would be great! Thanks!

推荐答案

您应该将obj强制转换为JsonArray而不是JsonObject,因为您的json文件的根目录为[].

You should cast your obj toJsonArrayinstead of JsonObject, because your json file has [] at the root.

这篇关于ClassCastException:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆