Adding an existing 2 dimensional array to a JSONObject

Hello @remka,

Example:

// https://www.geeksforgeeks.org/different-ways-to-declare-and-initialize-2-d-array-in-java/
// https://www.tutorialspoint.com/how-to-write-create-a-json-array-using-java
// https://stackoverflow.com/questions/30005939/how-to-put-object-into-jsonobject-properly
// https://www.javatpoint.com/program-to-remove-all-the-white-spaces-from-a-string

String[][] hexFrame = { 
                      {"0xA00000", "0xB00000", "0xC00000", "0xD00000"},
                      {"0x000000", "0x100000", "0x200000", "0x300000"}
                      };

JSONObject jsonObject = new JSONObject();

jsonObject.put("fps",  60);
jsonObject.put("myHexFrame", hexFrame);

println(jsonObject);
println();

String s = jsonObject.toString();
s = s.replaceAll("\\s+", "");
println(s);

saveJSONObject(jsonObject, "data/new.json");

Output:

"myHexFrame": [
    [
      "0xA00000",
      "0xB00000",
      "0xC00000",
      "0xD00000"
    ],
    [
      "0x000000",
      "0x100000",
      "0x200000",
      "0x300000"
    ]
  ],
  "fps": 60
}

{"myHexFrame":[["0xA00000","0xB00000","0xC00000","0xD00000"],["0x000000","0x100000","0x200000","0x300000"]],"fps":60}

:)

2 Likes