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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With