简体   繁体   English

尝试解析JSONArray时发生ClassCastException

[英]ClassCastException when trying to parse a JSONArray

When trying to parse the following JSON string using the json.simple library: 尝试使用json.simple库解析以下JSON字符串时:

[
    {"id" : "6d7662a9.f8ba04"},
    {"id" : "2da98cc2.145ba4"},
    {"id" : "45492640.a17d68"}
]

I get this exception: 我得到这个例外:

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

This is how I'm doing it: 这就是我的做法:

JSONArray json = (JSONArray) new JSONParser().parse(jsonString);

The JSON string is an array so not sure why that exception is thrown. JSON字符串是一个数组,因此不确定为什么会引发该异常。

There are several similar questions here but in their cases, they were trying to cast a JSONObject to a JSONArray so it makes sense that an exception is thrown but in this case, it looks correct. 这里有几个类似的问题,但是在他们的情况下,他们试图将JSONObject转换为JSONArray因此抛出异常是有意义的,但在这种情况下,它看起来是正确的。

-----------------EDITS----------------- ----------------- ----------------- EDITS

I added a line to print the class of the object, like this: 我添加了一行来打印对象的类,如下所示:

Object json = new JSONParser().parse(jsonString);
System.out.println(json.getClass());

That prints the following line: 打印以下行:

class org.json.simple.JSONArray

and in the next line, I have this if: 在下一行中,如果出现以下情况,则显示为:

if(json instanceof JSONArray) {
    System.out.println("This is a JSONArray");
}

But it does not access the if, so it is really weird, because first I check that the object is a JSONArray but it does not print "This is a JSONArray" 但是它不访问if,所以它真的很奇怪,因为首先我检查对象是否为JSONArray,但是它不打印"This is a JSONArray"

You say you are getting this: 您说您得到此:

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

Note that the fully qualified class names look identical. 请注意,完全限定的类名看起来相同。 If that message was transcribed accurately, and the names are identical 1 , it means that you have two versions of the JSONArray class loaded into your JVM! 如果该消息是准确的转录,名称相同的1,这意味着你有两个版本JSONArray加载到JVM类!

This is possible in a JVM where the application or the framework has multiple class loaders, and you have managed to load the class in more than one class loader. 在JVM中,应用程序或框架具有多个类加载器,并且您已经设法在多个类加载器中加载了类,这是可能的。 For example, this could happen if you have the JSON library JAR file BOTH in a web container's shared library directory AND in a webapp's WAR file. 例如,如果您在Web容器的共享库目录中同时在Webapp的WAR文件中同时拥有JSON库JAR文件,则可能会发生这种情况。

The thing is that the runtime type of a class is identified by the classes FQN and the classloader. 事实是,类的运行时类型由类FQN和类加载器标识。 Two classes with the same FQN loaded in different class loaders are different types, even if the bytecodes are identical. 即使字节码相同,在不同的类加载器中加载的具有相同FQN的两个类也属于不同类型。 This means that they are not assignment compatible, and it leads to weird (but correct!) class cast failures. 这意味着它们与分配不兼容,并且会导致奇怪的(但正确的!)类强制转换失败。

Solution: 解:

  1. Look at the web container file tree on your execution platform to find multiple copies of the JSON JAR file. 查看执行平台上的Web容器文件树,以查找JSON JAR文件的多个副本。
  2. Remove the problematic copy(s) from either the shared lib directory or all of the WAR files. 从共享lib目录或所有WAR文件中删除有问题的副本。

1 - A really obscure alternative explanation is that the names look the same due to "homoglyphs" but are actually different. 1-一个真正晦涩的替代解释是,由于“象形文字”,这些名称看起来相同,但实际上却不同。 But in this context, I think we can discount that explanation as implausible. 但是在这种情况下,我认为我们可以不合理地解释这种解释。

It should be like 应该像

    Object obj = new JSONParser().parse(jsonString);
    JSONArray json = new JSONArray();
json.add(obj);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM