java.lang.ClassCastException: org.json.simple.JSONArray 不能转换为 org.json.JSONArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20652284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:21:20  来源:igfitidea点击:

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

javaandroidjsonclasscastexceptionfilereader

提问by user2085965

I need to read a JsonArraydata from a file (on an SD card) and store it into a SQLite database.

我需要JsonArray从文件(在 SD 卡上)读取数据并将其存储到 SQLite 数据库中。

but I am getting a java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArrayexception while parsing data to JsonArray.

但是在将java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray数据解析为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..

请帮忙..

回答by Bjarke Freund-Hansen

Check your import statements at the top of your source file.

检查源文件顶部的导入语句。

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. At that point, the IDE would show you different possibilities of importing the class JSONArray. You have one in org.json.JSONArrayand one in 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.

如果您在 eclipse 或任何其他智能 IDE 中工作,那么当您开始键入以JSONArray. 那时,IDE 会向您展示导入类的不同可能性JSONArray。你一进org.json.JSONArray一进org.json.simple.JSONArray。后者是正确的,但您选择了第一个,您的 IDE 会自动在源 java 文件的顶部添加一个导入行。

From the exception the problem is quite apparent, the exception tells you that your objobject cannot be cast to a org.json.JSONArrayobject as it is really a org.json.simple.JSONArrayobject. (Note that there is a different between the two).

从异常中问题很明显,异常告诉您您的obj对象不能强制转换为org.json.JSONArray对象,因为它确实是一个org.json.simple.JSONArray对象。(请注意,两者之间存在差异)。

Update

更新

If you want to use org.json.JSONArrayinstead, you should use another class than jsonParseras this one is from the org.json.simplepackage. Currently you are mixing the two libraries, and that is what causes your troubles.

如果你想使用org.json.JSONArray,你应该使用另一个类,而不是jsonParser这个类来自org.json.simple包。目前您正在混合这两个库,这就是导致您遇到麻烦的原因。

回答by LoveToCode

You can first read the whole content of file into a 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。