I have a JsonObject and I need to add new values to it. The values are Strings. There is a method available JsonObject.put(), which takes two variables:
StringKey.JsonValuevalue.
How to convert the String, which is in the JSON format, to JsonValue so that I can add it to the JsonObject?
2 Answers
If you are using javax.json.JsonObject, then you may try:
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
for (Limit limit : limits) {
arrayBuilder.add(Json.createObjectBuilder().add(limit.getName(), limit.getValue()));
}
JsonObject result = Json.createObjectBuilder().add("AllLimits", arrayBuilder).build();
System.out.println(result);
// ... some code before enrich existing JsonObject...
JsonObjectBuilder builder = Json.createObjectBuilder(result);
builder.add("AditionalString", Json.createValue("My Computer Game Memory Limits"));
result = builder.build();
System.out.println(result);
However put(String, JsonValue) may result in UnsupportedOperationException if you use javax.json.JsonObject.
this:
JSONObject j=new JSONObject();
JSONObject j2=new JSONObject();
j2.put("XXX", "UUU");
j.put("one key", "one value");
j.put("2", j2);