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.

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


int[] intsArray = { 3, 0, -1 };

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

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