I'm trying to read a JSON file in which I have a series of integer data but when read tells me it can not convert from JSONObject to JSONArray
Part of the JSON file structure is:
{
"data": [
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
],
"time": 0.2
},
Code:
public static void main(String[] args) throws InterruptedException {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("C:\Carriots\dos.json"));
JSONObject jsonObject = (JSONObject) obj;
// loop array
JSONArray tag = (JSONArray) jsonObject.get("data");
Iterator iterator = tag.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
See Question&Answers more detail:
os