简体   繁体   中英

How do I create a JSONObject without keys?

I am trying to make my JSONObject into a format like the one below. I am getting confused on how we have the Yellow Room and we have the Blue Room , but they don't have a key?

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

This isn't my actual code, I am doing this with loops, but I would just like to understand this first.

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?

First You need to create a List of rooms, List of bookings and map of booking and map of room. Then You can add bookings to a bookingsList, bookingsList to a room and finally you can add that room to the roomsList. Refer below code,

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

Output

[{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}]}]

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

Output

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

You can create an array of JSON objects using the link below, https://docs.oracle.com/javame/8.0/api/json/api/com/oracle/json/JsonArray.html

You can find the screenshot below, 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM