简体   繁体   English

java.lang.ClassCastException:org.json.simple.JSONArray无法强制转换为org.json.JSONArray

[英]java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

I need to read a JsonArray data from a file (on an SD card) and store it into a SQLite database. 我需要从文件(在SD卡上)读取JsonArray数据并将其存储到SQLite数据库中。

but I am getting a java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray exception while parsing data to JsonArray . 但我得到一个java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray数据解析为JsonArrayjava.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray异常。

Data in the file is very huge, the whole data can not be stored in a single String or String Buffer or String Builder. 文件中的数据非常庞大,整个数据不能存储在单个String或String Buffer或String Builder中。

Here is my code. 这是我的代码。

FileReader fileReader = new FileReader(file_path);
Object obj = jsonParser.parse(fileReader);
JSONArray jsonArray = (JSONArray) obj; // Exception.

Please help.. 请帮忙..

Check your import statements at the top of your source file. 检查源文件顶部的import语句。

You probably have a line saying: 你可能有一句话说:

import org.json.JSONArray;

It should instead be: 它应该是:

import org.json.simple.JSONArray;

If you are working in eclipse, or any other intelligent IDE, the problem probably happened when you started typing the line starting with JSONArray . 如果您在eclipse或任何其他智能IDE中工作,当您开始键入以JSONArray开头的行时,可能会发生此问题。 At that point, the IDE would show you different possibilities of importing the class JSONArray . 此时,IDE将向您显示导入类JSONArray不同可能性。 You have one in org.json.JSONArray and one in org.json.simple.JSONArray . 你有一个在org.json.JSONArray ,一个在org.json.simple.JSONArray The latter is the right one, but you choose the first one and your IDE automatically added an import line at the top of your source java file. 后者是正确的,但您选择第一个,并且您的IDE会自动在源java文件的顶部添加一个导入行。

From the exception the problem is quite apparent, the exception tells you that your obj object cannot be cast to a org.json.JSONArray object as it is really a org.json.simple.JSONArray object. 从异常中问题非常明显,该异常告诉您无法将obj对象org.json.JSONArray转换为org.json.JSONArray对象,因为它实际上是一个org.json.simple.JSONArray对象。 (Note that there is a different between the two). (注意两者之间有不同)。

Update 更新

If you want to use org.json.JSONArray instead, you should use another class than jsonParser as this one is from the org.json.simple package. 如果你想使用org.json.JSONArray ,你应该使用另一个类而不是jsonParser因为这个类来自org.json.simple包。 Currently you are mixing the two libraries, and that is what causes your troubles. 目前你正在混合这两个库,这就是导致你的麻烦的原因。

You can first read the whole content of file into a String. 您可以先将文件的整个内容读入String。

FileInputStream fileInputStream = null;
String data="";
StringBuffer stringBuffer = new StringBuffer("");
try{
    fileInputStream=new FileInputStream(filename);
    int i;
    while((i=fileInputStream.read())!=-1)
    {
        stringBuffer.append((char)i);
    }
    data = stringBuffer.toString();
    csvFile.delete();
}
catch(Exception e){
        LoggerUtil.printStackTrace(e);
}
finally{
    if(fileInputStream!=null){  
        fileInputStream.close();
    }
}

Now You will have the whole content into String ( data variable ). 现在,您将把整个内容都放入String(数据变量)中。

JSONParser parser = new JSONParser();
org.json.simple.JSONArray jsonArray= (org.json.simple.JSONArray) parser.parse(data);

After that you can use jsonArray as you want. 之后,您可以根据需要使用jsonArray。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: 使用 java 读取 json 文件时,无法将 org.json.simple.JSONArray 转换为 org.json.simple.JSONObject 错误 - java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject error when reading json file iwith java 将org.json.JSONArray转换为org.json.simple.JSONArray - Convert org.json.JSONArray to org.json.simple.JSONArray ClassCastException:org.json.simple.JSONArray无法转换为org.json.simple.JSONObject - ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject Java org.json.simple.JsonArray无法获取JsonObjects - Java org.json.simple.JsonArray cannot get JsonObjects org.json.simple.JSONArray 不能转换为 org.json.simple.JSONObject - org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject “org.json.simple.JSONArray 无法转换为 org.json.simple.JSONObject” - “org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject” org.json.simple.JSONArray 无法转换为 class org.json.simple.JSONObject - org.json.simple.JSONArray cannot be cast to class org.json.simple.JSONObject android中的java.lang.NoSuchMethodError:org.json.JSONArray错误 - java.lang.NoSuchMethodError:org.json.JSONArray error in android org.json.JSONArray无法转换为JSONObject - org.json.JSONArray cannot be converted to JSONObject org.json.JSONException:值<jsonarray-here> org.json.JSONArray 类型无法转换为 JSONObject</jsonarray-here> - org.json.JSONException: Value <JSONarray-here> of type org.json.JSONArray cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM