可解码的 JSONSerialization 错误自定义对象 alamofire
Decodable JSONSerialization error custom object alamofire
我有一个使用 alamofire5 beta 的自定义 APIClient,它符合请求的 Codable 协议。
我正在尝试通过 httpBody (post) 发送自定义对象,但出现此错误:
Invalid type in JSON write (_SwiftValue)
这是我要发送的对象:
struct Complex: Codable {
var id: String
var name: String
var address: String
var zipcode: String
var amenities: [String]
var schedules: [ComplexSchedules]
init(id: String, name: String, address: String, zipcode: String, amenities: [String], schedules: [ComplexSchedules]) {
self.id = id
self.name = name
self.address = address
self.zipcode = zipcode
self.amenities = amenities
self.schedules = schedules
}
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case address = "address"
case zipcode = "zipcode"
case amenities = "amenities"
case schedules = "schedules"
}
struct TimeRange: Codable {
var from: String
var to: String
init(from: String, to: String) {
self.from = from
self.to = to
}
enum CodingKeys: String, CodingKey {
case from = "from"
case to = "to"
}
}
struct ComplexSchedules: Codable {
var day: String
var timeRanges: [TimeRange]
init(day: String, timeRanges: [TimeRange]) {
self.day = day
self.timeRanges = timeRanges
}
enum CodingKeys: String, CodingKey {
case day = "day"
case timeRanges = "time_ranges"
}
}
}
调用这个方法失败:
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: complex, options: [])
有什么想法吗?
您可能需要
do {
let data = try JSONEncoder().encode(complex)
urlRequest.httpBody = data
}
catch {
print(error)
}
as Codable
用于能够利用 JSONDecoder
和 JSONEncoder
而不是与 JSONSerialization
一起使用,后者需要像原始 [=15 这样的非自定义对象=]
我有一个使用 alamofire5 beta 的自定义 APIClient,它符合请求的 Codable 协议。 我正在尝试通过 httpBody (post) 发送自定义对象,但出现此错误:
Invalid type in JSON write (_SwiftValue)
这是我要发送的对象:
struct Complex: Codable {
var id: String
var name: String
var address: String
var zipcode: String
var amenities: [String]
var schedules: [ComplexSchedules]
init(id: String, name: String, address: String, zipcode: String, amenities: [String], schedules: [ComplexSchedules]) {
self.id = id
self.name = name
self.address = address
self.zipcode = zipcode
self.amenities = amenities
self.schedules = schedules
}
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case address = "address"
case zipcode = "zipcode"
case amenities = "amenities"
case schedules = "schedules"
}
struct TimeRange: Codable {
var from: String
var to: String
init(from: String, to: String) {
self.from = from
self.to = to
}
enum CodingKeys: String, CodingKey {
case from = "from"
case to = "to"
}
}
struct ComplexSchedules: Codable {
var day: String
var timeRanges: [TimeRange]
init(day: String, timeRanges: [TimeRange]) {
self.day = day
self.timeRanges = timeRanges
}
enum CodingKeys: String, CodingKey {
case day = "day"
case timeRanges = "time_ranges"
}
}
}
调用这个方法失败:
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: complex, options: [])
有什么想法吗?
您可能需要
do {
let data = try JSONEncoder().encode(complex)
urlRequest.httpBody = data
}
catch {
print(error)
}
as Codable
用于能够利用 JSONDecoder
和 JSONEncoder
而不是与 JSONSerialization
一起使用,后者需要像原始 [=15 这样的非自定义对象=]