- 1st of all, you’re gonna need a function which converts your multidimensional array to a JSONArray container.
- Inside a multi-loop, use method append() to add more JSONArray containers, and finally the values themselves inside the innermost dimension:
append() / Reference / Processing.org - Then you can use saveJSONArray() to save it as a JSON file:
saveJSONArray() / Reference / Processing.org
/**
* Conversion Arr3d to JSONArr3d (v0.1)
* GoToLoop (2019/Dec/14)
* Discourse.Processing.org/t/saving-storing-a-multidimensional-array/16391/3
*/
static final String FILENAME = "JArr3D.json", OPT = "compact";
double[][][] my3dArr = new double[4][2][3];
JSONArray myJArr;
void setup() {
myJArr = arr3dToJArr(my3dArr);
println(myJArr);
saveJSONArray(myJArr, dataPath(FILENAME), OPT);
exit();
}
static final JSONArray arr3dToJArr(final double[][][] arr3d) {
final JSONArray jarr3d = new JSONArray();
for (final double[][] arr2d : arr3d) {
final JSONArray jarr2d = new JSONArray();
jarr3d.append(jarr2d);
for (final double[] arr1d : arr2d) {
final JSONArray jarr1d = new JSONArray();
jarr2d.append(jarr1d);
for (final double d : arr1d) jarr1d.append(d);
}
}
return jarr3d;
}
I did the Arr3d to JArray part. You just need to do the reverse. ![]()