Simple task but I couldn't find a way to do it.
My output JSON needs to be
{
"id" : "somestring",
"nums" : [44,31,87,11,34]
}
I'm using the javax.json library with JSONObject / JsonArray. There is a List<Integer> which comes in with the values for the 2nd field. These aren't objects, these are plain numbers. I don't know how to get a JSONValue from an Integer.
Map<String, Object> config = new HashMap<String, Object>();
JsonBuilderFactory factory = Json.createBuilderFactory(config);
JsonArray jsonArray = (JsonArray) Json.createArrayBuilder();
for (Integer num: nums) // Assume a List<Integer> nums
jsonArray.add(..); // What to do here? JSONValue from an Integer?
// Can't do jsonArray.add(num)
// Final Object
JsonObject value = factory.createObjectBuilder()
.add("id", id)
.add("nums", jsonArray); // Link up jsonArray to the 2nd Add
