简单的任务,但我找不到办法。
我的输出JSON需要{
"id" : "somestring",
"nums" : [44,31,87,11,34]}
我正在使用/ 的javax.json库。还有一个,它与在第二字段中的值。这些不是对象,这些是普通数字。我不知道如何从Integer获取JSONValue。JSONObjectJsonArrayListMap config = new HashMap();
JsonBuilderFactory factory = Json.createBuilderFactory(config);
JsonArray jsonArray = (JsonArray) Json.createArrayBuilder();
for (Integer num: nums) // Assume a List 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
注意 - 无法直接添加整数