Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.json library anywhere. Have not imported it either.

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

like image 839
simar kaur Avatar asked Oct 23 '25 10:10

simar kaur


1 Answers

The error occurred because you tried to convert number into 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.

like image 144
H.Ostwal Avatar answered Oct 26 '25 00:10

H.Ostwal