Alchemik123
Alchemik123

Reputation: 33

How to change JsonObject in JsonArray?

I'm using javax.json and when I tried change jsonObject in my jsonArray:

String jsonString = "[{\"name\":\"xyz\"," +
                        "\"URL\":\"http://example.com\"}]";
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonArray jsonArray = jsonReader.readArray();

String jsonNewString = "{\"name\":\"zyx\","
                         + "\"URL\":\"http://example2.com\"}]";   
jsonReader = Json.createReader(new StringReader(jsonNewString));
JsonObject jsonObject = jsonReader.readObject();
jsonReader.close();

jsonArray.remove(0);
jsonArray.add(0, jsonObject);

I got this exception:

java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)

I also tried: jsonArray.set(0, jsonObject);, and got the same UnsupportedOperationException.

Upvotes: 3

Views: 3109

Answers (2)

Oussama ZAGHDOUD
Oussama ZAGHDOUD

Reputation: 2159

JsonObject and JsonArray are immutable so you cannot modify the object , you can use this example and try to inspire from it :

creation of a new JsonObject who contains the same values and add some elements to it ..

Example :

    import java.io.ByteArrayInputStream;
    import java.io.InputStream;

    import javax.json.Json;
    import javax.json.JsonArray;
    import javax.json.JsonArrayBuilder;
    import javax.json.JsonObject;
    import javax.json.JsonObjectBuilder;
    import javax.json.JsonReader;
    import javax.json.JsonValue;


    public class Jsonizer {
    public static void main(String[] args) {

    try {
    String s = "{\"persons\": [ {\"name\":\"oussama\",\"age\":\"30\"}, {\"name\":\"amine\",\"age\":\"25\"} ]}";
    InputStream is = new ByteArrayInputStream(s.getBytes());
    JsonReader jr = Json.createReader(is);
    JsonObject jo = jr.readObject();
    System.out.println("Before :");
    System.out.println(jo);
    JsonArray ja = jo.getJsonArray("persons");

    InputStream targetStream = new ByteArrayInputStream("{\"name\":\"sami\",\"age\":\"50\"}".getBytes());
    jr = Json.createReader(targetStream);
    JsonObject newJo = jr.readObject();
    JsonArrayBuilder jsonArraybuilder = Json.createArrayBuilder();
    jsonArraybuilder.add(newJo);
    
    for (JsonValue jValue : ja) {
        jsonArraybuilder.add(jValue);
    }
    
    ja = jsonArraybuilder.build();
    JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
    jsonObjectBuilder.add("persons", ja);
    JsonObject jsonAfterAdd = jsonObjectBuilder.build();
    System.out.println("After");
    System.out.println(jsonAfterAdd.toString());

} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}
}

Output :

     Before :
     {"persons":[{"name":"oussama","age":"30"},{"name":"amine","age":"25"}]}
     After
     {"persons":[{"name":"sami","age":"50"},{"name":"oussama","age":"30"},{"name":"amine","age":"25"}]}

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

The javadoc of JsonArray states

JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array.

You can't change it. Create a new one with the value(s) you want.

Upvotes: 4

Related Questions