java.lang.ClassCastException:org.json.simple.JSONArray不能转换为org.json.JSONArray [英] java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

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

问题描述

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

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

但我得到一个 java.lang.ClassCastException:org.json.simple.JSONArray不能转换为org.json.JSONArray 除了在分析数据 JsonArray

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.

文件中的数据是非常巨大的,整个数据不能存储在一个单一的字符串或字符串缓冲或字符串构建器。

Data in the file is very huge, the whole data can not be stored in a single String or String Buffer or String Builder.

下面是我的code。

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

请帮助..

推荐答案

检查import语句在源文件的顶部。

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

您可能有一个行说:

import org.json.JSONArray;

这应该不是是:

It should instead be:

import org.json.simple.JSONArray;

如果您在使用Eclipse或任何其它智能IDE的问题,当你开始打字开始 JSONArray 行可能发生。在这一点上,IDE会告诉你导入之类的不同的可能性 JSONArray 。你有一个在 org.json.JSONArray ,一个在 org.json.simple.JSONArray 。后者是正确的,但你选择了第一个,你的IDE会自动在你的代码的Java文件的顶部添加一个导入线。

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.JSONArray and 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.

从异常的问题相当明显,除了告诉你,你的 OBJ 对象不能被转换为 org.json.JSONArray 对象实在是一个 org.json.simple.JSONArray 对象。 (注意,有两者之间的不同)。

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. (Note that there is a different between the two).

更新

如果你想使用 org.json.JSONArray 相反,你应该使用另一个类比 jsonParser ,因为这一个是从 org.json.simple 包。您目前混合两个库,这是什么原因导致你的烦恼。

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. Currently you are mixing the two libraries, and that is what causes your troubles.

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

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