I have a JSON file that, among other things, contains various 2D arrays. Here is a link to a copy of the file. I want to load things such as "floor" into an int[][] array. I can't seem to find any way to do that. I might also be storing the array incorrectly, I'm not sure. I create the JSON file with a python script.
When I use processing's "loadJSONObject" function and access "floor", it says that it is a "processing.data.JSONArray" but I can't index it like a normal array. If I try to, it gives the error "The type of the expression must be an array type but it resolved to Object"
If I try to index it with ".getIntArray" like in this example, it gives the error "The function getIntArray() does not exist."
Here is some code, since I can't link pastebin without it:
JSONObject currentLevel;
currentLevel = loadJSONObject("assets/levels/test_level.json");
println(currentLevel.get("floor").getIntArray());
Any help is appreciated, thanks.
CodePudding user response:
From the documentation, one level up:
getJSONArray()
: Retrieves theJSONArray
with the associated index value
After we get the floor
data (e.g. floor = currentlevel.getJSONArray("floor")
), it is itself an array of arrays of integers. Processing represents this with its JSONArray
type, which apparently does not support indexing. We also cannot convert it to a plain array of integers, because it doesn't represent one - in the same way that we cannot treat an int[][]
as if it were an int[]
in Java.
Instead: use getJSONArray
on floor
to get the individual arrays of integers; and those can then be converted using getIntArray
.
CodePudding user response:
You need to do it like this: using getJSONArray()
JSONObject currentLevel;
currentLevel = loadJSONObject("assets/levels/test_level.json");
JSONArray array = currentLevel.getJSONArray("floor");
print(array)
Ref: