簡體   English   中英

如何創建不帶鍵的JSONObject?

[英]How do I create a JSONObject without keys?

我正在嘗試將我的JSONObject轉換為以下格式。 我對如何擁有Yellow RoomBlue 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"}]}]

您可以使用下面的鏈接https://docs.oracle.com/javame/8.0/api/json/api/com/oracle/json/JsonArray.html創建JSON對象數組

您可以在下面找到屏幕截圖, 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM