我正在尝试将我的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?
答案 0 :(得分:2)
首先,您需要创建房间列表,预订列表以及预订地图和房间地图。然后,您可以将预订添加到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"}]}]
答案 1 :(得分:0)
您可以使用下面的链接创建JSON对象数组, https://docs.oracle.com/javame/8.0/api/json/api/com/oracle/json/JsonArray.html