简单的任务,但我找不到办法。
我的输出JSON需要
{ "id" : "somestring", "nums" : [44,31,87,11,34]}
我正在使用/ 的javax.json
库。还有一个,它与在第二字段中的值。这些不是对象,这些是普通数字。我不知道如何从Integer获取JSONValue。JSONObject
JsonArray
List<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
注意 - 无法直接添加整数
海绵宝宝撒
慕尼黑5688855
相关分类