我试图使我的JSONObject成为下面的格式。我对如何拥有Yellow Room和我们具有Blue Room感到困惑,但是他们没有钥匙?

[ { "room": "Yellow Room", "Bookings": [
    { "customer": "John", "age": "21" },
    { "customer": "Bob", "age": "33" }
    ] },
  { "room": "Blue Room", "Bookings": [
    { "customer": "Mike", "age": "56" },
    { "customer": "Billy", "age": "37" }
    ] }
]


这不是我的实际代码,我正在使用循环执行此操作,但是我想首先了解一下。

JSONObject rooms = new JSONObject();
rooms.put("name", "Yellow room");
JSONObject bookings = new JSONObject();
JSONObject booking = new JSONObject();
booking.put("customer", "John");
booking.put("age", "21");
bookings.put("bookings", booking);
booking.put("customer", "Bob");
booking.put("age", "33");
bookings.put("bookings", booking);
// Now I am lost, What do I do?

rooms = new JSONObject();
rooms.put("name", "Blue room");
bookings = new JSONObject();
booking = new JSONObject();
booking.put("customer", "Mike");
booking.put("age", "56");
bookings.put("bookings", booking);
booking.put("customer", "Billy");
booking.put("age", "37");
bookings.put("bookings", booking);
// Now I am lost, What do I do?

最佳答案

首先,您需要创建房间列表,预订列表和预订地图以及房间地图。然后,您可以将预订添加到bookingsList,将bookingsList添加到房间,最后您可以将该房间添加到roomsList。参考下面的代码,

Map<String, Object> room = new HashMap<>(); //put room details to this
Map<String, Object> booking = new HashMap<>(); //put booking details to this
List<Map<String, Object>> list = new ArrayList<>(); //put rooms to this
List<Map<String, Object>> bookingList = new ArrayList<>(); //put bookings to this

booking.put("customer", "John"); //creating booking 1
booking.put("age", "21");
bookingList.add(booking); //adding booking bookingList

booking.put("customer", "Bob"); //creating booking 2
booking.put("age", "33");
bookingList.add(booking); //adding booking to bookingList

room.put("name", "Yellow room"); //adding name to room list
room.put("bookings", bookingList); //adding bookings to room list
list.add(room);


booking.put("customer", "Mike");
booking.put("age", "56");
bookingList.add(booking);

booking.put("customer", "Billy");
booking.put("age", "37");
bookingList.add(booking);

room.put("name", "Blue room"); //adding second name to room list
room.put("bookings", bookingList); //adding second bookings to room list
list.add(room);

System.out.println(list);


输出量

[{name=Blue room, bookings=[{age=37, customer=Billy}, {age=37, customer=Billy}, {age=37, customer=Billy}, {age=37, customer=Billy}]}, {name=Blue room, bookings=[{age=37, customer=Billy}, {age=37, customer=Billy}, {age=37, customer=Billy}, {age=37, customer=Billy}]}]


使用JsonObject

JSONObject rooms = new JSONObject();
JSONObject booking = new JSONObject();
JSONArray list = new JSONArray();
JSONArray bookingList = new JSONArray();

booking.put("customer", "John"); //creating booking 1
booking.put("age", "21");
bookingList.put(booking); //adding booking bookingList

booking.put("customer", "Bob"); //creating booking 2
booking.put("age", "33");
bookingList.put(booking); //adding booking to bookingList

rooms.put("name", "Yellow room"); //adding name to room list
rooms.put("bookings", bookingList); //adding bookings to room list
list.put(rooms);


booking.put("customer", "Mike");
booking.put("age", "56");
bookingList.put(booking);

booking.put("customer", "Billy");
booking.put("age", "37");
bookingList.put(booking);

rooms.put("name", "Blue room"); //adding second name to room list
rooms.put("bookings", bookingList); //adding second bookings to room list
list.put(rooms);

System.out.println(list);


输出量

[{"name":"Blue room","bookings":[{"age":"37","customer":"Billy"},{"age":"37","customer":"Billy"},{"age":"37","customer":"Billy"},{"age":"37","customer":"Billy"}]},{"name":"Blue room","bookings":[{"age":"37","customer":"Billy"},{"age":"37","customer":"Billy"},{"age":"37","customer":"Billy"},{"age":"37","customer":"Billy"}]}]

关于java - 如何创建不带键的JSONObject?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56746243/

10-13 09:21