Convert String to javax.json.JsonValue

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:

  1. String Key.
  2. JsonValue value.

How to convert the String, which is in the JSON format, to JsonValue so that I can add it to the JsonObject?

2

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); 
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like