How do I save an array of integers to json object/array

If I had an array of integers, like so: int array[] = {1,2,3,4,5};

How would I convert this array to JSON so that it is in this format
{ "array" : [ 1, 2, 3, 4, 5 ] }

I am not sure what’s best inside my Processing(Java) code.

The documentation is not clear to me as to how to do this.
https://processing.org/reference/JSONArray.html
https://processing.org/reference/JSONObject.html

If anyone can help, that’ll be most greatly appreciated.

Thanks

Processing.GitHub.io/processing-javadocs/core/processing/data/JSONArray.html#JSONArray-processing.data.IntList-

int[] intsArray = { 3, 0, -1 };
println(str(intsArray));
println();

JSONArray intsJson = new JSONArray(new IntList(intsArray));
println(intsJson);

exit();
1 Like

Thanks, that’s most helpful.

I like the way you used a new IntList within a new JSONArray - that’s a neat trick.

new JSONArray(new IntList(intsArray));
1 Like