Попробуйте использовать Swift 4 Encodable для создания JSON.
Пример:
struct Body: Encodable {
var from: String
var to: String
var rooms: [Room]
init(from: String, to: String, rooms: [Room]) {
self.from = from
self.to = to
self.rooms = rooms
}
}
struct Room: Encodable {
var age: Int
var adults: Int
var children: Int
init(age: Int, adults: Int, children: Int) {
self.age = age
self.adults = adults
self.children = children
}
}
let body = Body(from: "Amsterdam", to: "Dubai", rooms: [Room(age: 22, adults: 1, children: 0), Room(age: 54, adults: 0, children: 1)])
let encoded = try JSONEncoder().encode(body)
//String(data: encoded, encoding: .utf8)
//{"from": "Amsterdam", "to": "Dubai", "rooms": [{"age":22,"children":0,"adults":1},{"age":54,"children":1,"adults":0}]}