[Solved] create a JSON file in java with array


A little helper makes life easier:

public static JSONArray jsonArray(Object... values) {
    JSONArray arr = new JSONArray();
    arr.addAll(Arrays.asList(values));
    return arr;
}

Then:

JSONObject obj = new JSONObject();
obj.put("data", jsonArray(jsonArray("1", "YES", "sp_1", "1", "xxx"),
                          jsonArray("2", "NO" , "sp_2", "2", "yyyy"),
                          jsonArray("3", "YES", "sp_3", "2", "zzzz")));
System.out.println(obj.toJSONString());

Output

{"data":[["1","YES","sp_1","1","xxx"],["2","NO","sp_2","2","yyyy"],["3","YES","sp_3","2","zzzz"]]}

1

solved create a JSON file in java with array