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.
The javadoc of JsonArray states
JsonArrayrepresents 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.
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