Problems adding Jsonobject to Jsonarray

Asked

Viewed 103 times

0

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;

// código

   JsonObject object = Json.createObjectBuilder().build();
   JsonArray array = Json.createArrayBuilder().build();

   JsonArray tweets = getTweets();

   for (JsonValue tweet : tweets) {
       object = (JsonObject) tweet;
       array.add(object);
   }
   return array;

I take this exception:

java.lang.UnsupportedOperationException

I also took a look at java documentation but it didn’t help...

1 answer

1


According to the documentation, the JsonArray represents a json array immutable. That is, once built, it cannot be modified.

Instead of working with the JsonArray directly, use the JsonArrayBuilder:

JsonArrayBuilder builder = Json.createArrayBuilder();
JsonArray tweets = getTweets();

for (JsonValue tweet : tweets) {
    builder.add(tweet);
}
return builder.build();
  • Thank you, myself, yesterday, I had already done it here on the project and I had forgotten. I found the answer right now, between the classes of my code.Anyway now this clarified.. That’s what gives little sleep... Lapses of memory

Browser other questions tagged

You are not signed in. Login or sign up in order to post.