Problems adding JsonObject to JsonArray

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

...


   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 get this exception:

java.lang.UnsupportedOperationException

I also took a look at the java documentation but it did not help. .

    
asked by anonymous 16.04.2018 / 17:31

1 answer

0

According to documentation , JsonArray represents a json array < strong> immutable . That is, after it is built, it can not be modified.

Instead of working with JsonArray directly, use JsonArrayBuilder :

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

for (JsonValue tweet : tweets) {
    builder.add(tweet);
}
return builder.build();
    
16.04.2018 / 18:25