gpt4 book ai didi

java - 如何创建没有键的 JSONObject?

转载 作者:行者123 更新时间:2023-11-30 01:49:10 26 4
gpt4 key购买 nike

我正在尝试将 JSONObject 转换为如下格式。我很困惑我们如何拥有黄色房间蓝色房间,但他们没有 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" }
] }
]

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

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?

最佳答案

您可以使用下面的链接创建 JSON 对象数组, https://docs.oracle.com/javame/8.0/api/json/api/com/oracle/json/JsonArray.html

您可以找到下面的屏幕截图, enter image description here

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com