java org.glassfish.json.JsonStringImpl 不能转换为 javax.json.JsonArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29562232/
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-11-02 15:30:25  来源:igfitidea点击:

org.glassfish.json.JsonStringImpl cannot be cast to javax.json.JsonArray

javajsonglassfish

提问by simar kaur

I have this piece of code:

我有这段代码:

JsonObject fg = null;
try{
      fg =  message.getJson();  //gettng the JSON object from the client
      System.out.println("JSONGOBJECT IS HERE:   "+fg);

      JsonArray arr1 =  fg.getJsonArray("to");  //I get the exception here
      int size =  arr1.length();
      arrays = new ArrayList<String>();
      for(Object o : arr1)
      {
        String user=(o == null ? null : o.toString());
           if (user != null) {
                 arrays.add(user);
           }
       }
       catch(Exception e)
       {
           System.out.println("Exeption HERE: "+e);
       }

I get this exception while I try to access value for "to" parameter from JsonArray. I am not using org.glassfish.jsonlibrary anywhere. Have not imported it either.

当我尝试从JsonArray. 我没有org.glassfish.json在任何地方使用图书馆。也没有导入。

I have imported only javax.jsonin my current code. How do I fix this ?

我只导入javax.json了我当前的代码。我该如何解决 ?

回答by H.Ostwal

The error occurred because you tried to convert numberinto string.

因为你想转换时发生错误number进入string

This will work for sure.

这肯定会奏效。

for (int i = 0; i < jsonArray.size(); i++) 
jsonArray.getJsonObject(i).get(key).toString();

Get object in a generic way.

以通用方式获取对象。

回答by young chisango

Not sure what the data structure of the message object you are getting from the client, to determine if the "to" object is an array or string. You could provide more info here. The exception may be due to the fact that the "to" key is mapped to a JsonString ( eg {to:"thisthat"}, as opposed to an array (eg {to:["this", that"]}). If this is the case , the attempted casting of the JsonString to JsonArray fails, hence the exception. May not be spot on here, but you need to check the structure of the message, especially the "to" field.

不确定您从客户端获取的消息对象的数据结构是什么,以确定“to”对象是数组还是字符串。您可以在此处提供更多信息。例外可能是由于“to”键映射到 JsonString(例如 {to:"thisthat"},而不是数组(例如 {to:["this", that"]})。如果是这种情况,则尝试将 JsonString 转换为 JsonArray 失败,因此出现异常。可能不在此处,但您需要检查消息的结构,尤其是“to”字段。

With regards to the org.glassfish.json, it is most likely due to your runtime environment using a glassfish JSON implementation ( by default ). This may be included as a jar file ( class loading) . The json package (javax.json) you import in your code is only an interface. And it will allow you to compile the code. To run the code, you will need a java package implementation (jar)( any for that matter) that implements the javax.json interfaces). You can explicitly configure your runtime environment to use a specific json implementation package, or your environment may have a default configuration for javax.json

关于 org.glassfish.json,很可能是由于您的运行时环境使用了 glassfish JSON 实现(默认情况下)。这可以作为 jar 文件(类加载)包含在内。您在代码中导入的 json 包 (javax.json) 只是一个接口。它将允许您编译代码。要运行代码,您将需要一个实现 javax.json 接口的 java 包实现 (jar)(任何)。您可以显式配置您的运行时环境以使用特定的 json 实现包,或者您的环境可能具有 javax.json 的默认配置

Hope this helps.

希望这可以帮助。

回答by Tarun Gupta

As mentioned by young chisango you might not have included json-api implementation.

正如年轻的 chisango 所提到的,您可能没有包含 json-api 实现。

If you want to run an application with JSON Processing API, declare a dependency on json-api implementation in your maven project.

如果您想使用 JSON Processing API 运行应用程序,请在您的 maven 项目中声明对 json-api 实现的依赖。

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.0.4</version>
</dependency>

回答by Dilan

The issue is not because you are missing an import, as that would not cause glassfish to be triggered. As mentioned above, the glassfish implementation is called from the javax.json and that is not a problem.

问题不是因为您缺少导入,因为这不会导致 glassfish 被触发。如上所述,glassfish 实现是从 javax.json 调用的,这不是问题。

The problem is caused by not having an array in the JsonObject to convert to JsonArray.

该问题是由于 JsonObject 中没有要转换为 JsonArray 的数组引起的。

One possible example to this could be that in your javascript the array may be declared as an ArrayBuffer rather than an Array.

一个可能的例子是,在你的 javascript 中,数组可能被声明为一个 ArrayBuffer 而不是一个数组。

Go in your javascript to make sure you have an array, and then print out your JsonObject to see what is in it to fix your problem.

进入您的 javascript 以确保您有一个数组,然后打印出您的 JsonObject 以查看其中的内容来解决您的问题。

回答by Allen Cypher

I fixed a similar problem by changing my code to use getBooleaninstead of getJsonArray. So I would recommend trying different datatypes until you find one that works.

我通过更改我的代码来使用getBoolean而不是getJsonArray. 所以我建议尝试不同的数据类型,直到找到一种有效的数据类型。