【问题标题】:Writing into JSONArray写入 JSONArray
【发布时间】:2017-04-11 15:44:56
【问题描述】:

我正在读取 JSON 文件,取出值并进行一些更改。

基本上我在数组中添加了一些值。之后,我想将其写回文件中。当我将JSONArray 写回文件时,写为字符串而不是JSONArray 对象。怎样才能写好?

在以下代码中,我正在写入 JSON 文件:

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();

如您所见,它被写成字符串,当我想读回它时它给我带来了问题。

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

文件阅读器:

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));
}

错误:java.lang.ClassCastException:无法转换 java.lang.String 到 org.json.simple.JSONObject

在向数组输入新值后发生错误(引入日期和开始时间)。它被写为字符串,当我尝试再次读取它时,给我一个错误说我无法解析它,因为数组上有一个字符串。

输入文件:

{
"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"}

         ]
}

【问题讨论】:

  • 您能提供输入文件的内容吗?或者至少是一个相关的 sn-p
  • 还有Reservation类的代码
  • 完成。感谢支持。

标签: java json


【解决方案1】:

问题解决了!我没有创建 JSONObject,所以我直接向 JSON Array 引入了一个对象,它强制与 String 进行对话。

这是问题的解决方法:

 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);

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2017-05-31
    • 2020-01-23
    • 2021-03-07
    • 2014-06-09
    • 1970-01-01
    相关资源
    最近更新 更多