André Costa
André Costa

Reputation: 11

Writing into JSONArray

I'm reading JSON file, taking out the values and doing some changes.

Basically I added some values to array. After that I want to write it back on a file. When I write JSONArray back to file is written as string and not JSONArray object. How can I write it well?

In the following code I'm writing into JSON file:

JSONArray rooms = (JSONArray) jsonObject.get("rooms");
for (int i = 0; i < rooms.size(); i++) {
    JSONObject room = (JSONObject) rooms.get(i);
    String roomName = (String) room.get("roomName");
    System.out.println("RoomName: " + roomName + " Size: " + "O: " + oldRoomListSize.get(roomName) + " N: " + roomList.get(roomName).getRoomBook().size());
    if(oldRoomListSize.get(roomName) < roomList.get(roomName).getRoomBook().size()) {
        int n = roomList.get(roomName).getRoomBook().size() - oldRoomListSize.get(roomName);
        for (int j = n; j > 0; j--) {
            int lenght = roomList.get(roomName).getRoomBook().size();

            JSONArray schedule = (JSONArray) room.get("schedule");
            Reservation r = roomList.get(roomName).getRoomBook().get(lenght-j);
            schedule.add(r);
        }
    }
}

fileWriter.write(jsonObject.toJSONString());
fileWriter.close();

As you can see, is being written as string and it bring me problems when I want to read it back.

"schedule":["{Day: 1 - Start Time: 10}"]

File Reader:

JSONArray schedule = (JSONArray) room.get("schedule");
for (int j = 0; j < schedule.size(); j++) {
    JSONObject s = (JSONObject) schedule.get(j);
    String day = (String) s.get("day");
    String startTime = (String) s.get("startTime");
    lRoom.setRoomBook(Integer.parseInt(day), Integer.parseInt(startTime));
}

Error: java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONObject

Error occurs, after enter new value to array (Introduced day and start time). It's writen as string, and when i try to read it again, give me a error saying i cant parse it since there's a string on array.

Input file:

{
"rooms":[
        {"maxOccupants":"10",
         "schedule":[{"startTime":"1","day":"10"},{"startTime":"20","day":"20"},{"startTime":"11","day":"122017"}],
         "tv":"false",
         "mobilePhone":"false",
         "projector":"true",
         "buildID":"1",
         "floor":"2",
         "roomName":"room1"},

         {"maxOccupants":"4",
         "schedule":[{"startTime":"10","day":"1"},{"startTime":"11","day":"122017"},{"startTime":"11","day":"15"}],
         "tv":"false",
         "mobilePhone":"false",
         "projector":"false",
         "buildID":"1",
         "floor":"2",
         "roomName":"room2"},

         {"maxOccupants":"5",
         "schedule":[{"startTime":"1","day":"10"},{"startTime":"11","day":"122017"}],
         "tv":"false",
         "mobilePhone":"false",
         "projector":"true",
         "buildID":"2",
         "floor":"3",
         "roomName":"room3"}

         ]
}

Upvotes: 0

Views: 296

Answers (1)

Andr&#233; Costa
Andr&#233; Costa

Reputation: 11

Problem solved! I was not creating a JSONObject, so I was introducing a directly object to JSON Array and it forced conversation to String.

Here is the resolution of the problem:

 JSONObject jsonObj = new JSONObject();
 String startTime = String.valueOf(r.getStartTime());
 String day = String.valueOf(r.getDay());

 jsonObj.put("startTime", startTime);
 jsonObj.put("day", day);
 schedule.add(jsonObj);

Upvotes: 1

Related Questions