Alamofire + Decodable - 如何将 responseJSON 转换为 NSData
Alamofire + Decodable - how turn responseJSON to NSData
我很确定我的问题很容易解决,但我找不到任何解决方案。
所以我有 Alamofire 请求并且在处理数据类型时遇到了麻烦。我有那么多'printing out'只是为了一步步查看我得到了什么数据
Alamofire.request(URL, method: .get, headers: headers).responseJSON { response in
switch responseJSON.result {
case .success(let value):
print(type(of: value)) //__NSDictionaryI
print(value)
print(type(of:responseJSON)) //DataResponse<Any>
print(responseJSON) . //SUCCESS: {"billing_addresses" = (...
print(responseJSON.value as Any) . //Optional({...
//print(responseJSON.value as! [[String:Any]]) . //Could not cast value of type '__NSDictionaryI' (0x10b9fb508) to 'NSArray' (0x10b9fb008).
do {
let decoder = JSONDecoder()
let model = try decoder.decode(Info.self, from: value as! Data) //Decode JSON Response Data
print(model.id)
} catch let parsingError {
print("Error", parsingError)
}
现在我有一个错误:**Could not cast value of type '__NSSingleEntryDictionaryI' (0x10d240f78) to 'NSData' (0x10d241090).**
responseJSON 的值为:
(我不确定这个值是否正确,因为当我检查 Postman 时所有字符串都是双引号,并且 "is_default" 的值是 true/false,而不是 0/1。但是在 Xcode 中,我在控制台中得到了这个。所以 responseJSON 中可能有问题?..)
地址可能是零个,也可能是多个。
{
"id": 40128,
"username": "test6",
"email": "test6@on.com",
"billing_addresses": [
{
"address_name": null,
"country_code": "US",
"first_name": "Ted",
"last_name": "Qqqq",
"company_name": "",
"address_line1": "308 Sea Lane",
"address_line2": "",
"city": "QQQQ",
"state": "FL",
"postcode": "32000",
"email_address": "test6@on.com",
"phone_number": "11111111",
"is_default_for_billing": true
}
],
"shipping_addresses": [
{
"address_name": null,
"country_code": "US",
"first_name": "Ted",
"last_name": "Qqqq",
"company_name": "",
"address_line1": "308 Sea Lane",
"address_line2": "",
"city": "QQQQ",
"state": "FL",
"postcode": "32000",
"is_default_for_shipping": true
}
]
}
这是模型
struct Info : Decodable {
let id: Int
let email: String
let username: String
let billing_addresses: Billings
let shipping_addresses: Shippings
}
struct Billings: Decodable{
let address_name: String
let country_code: String
let first_name: String
let last_name: String
let company_name: String
let address_line1: String
let address_line2: String
let city: String
let state: String
let postcode: String
let email_address: String
let phone_number: String
let is_default_for_billing: Bool
}
struct Shippings:Decodable{
let address_name: String
let country_code: String
let first_name: String
let last_name: String
let company_name: String
let address_line1: String
let address_line2: String
let city: String
let state: String
let postcode: String
let is_default_for_shipping: Bool
}
如果我尝试使用带有 value
作为参数的 SwiftyJSON,我会遇到一个错误,即 Any
不能是 Data
,我真的不知道该怎么办。
responseJSON.result.value
returns 反序列化的集合类型,在你的例子中是字典 [String:Any]
要使用 JSONDecoder
,您需要 response.data
中的原始数据
let model = try decoder.decode(Info.self, from: response.data) //Decode JSON Response Data
考虑到你会 运行 解码错误:billing_addresses
和 shipping_addresses
是数组
let billing_addresses: [Billings]
let shipping_addresses: [Shippings] // better name both structs in singular form (Billing, Shipping)
一些值可以是数字而不是字符串。
无论如何建议使用convertFromSnakeCase
密钥解码策略来摆脱难看的snake_case名字。
编辑:
这是带有驼峰命名和单数形式的结构,您必须添加
decoder.keyDecodingStrategy = .convertFromSnakeCase
struct Info : Decodable {
let id: Int
let email: String
let username: String
let billingAddresses: [Billing]
let shippingAddresses: [Shipping]
}
struct Billing : Decodable {
let addressName: String?
let countryCode, firstName, lastName, companyName: String
let addressLine1, addressLine2, city, state, postcode: String
let emailAddress, phoneNumber: String
let isDefaultForBilling: Bool
}
struct Shipping : Decodable {
let addressName: String?
let countryCode, firstName, lastName, companyName: String
let addressLine1, addressLine2, city, state, postcode: String
let isDefaultForShipping: Bool
}
我很确定我的问题很容易解决,但我找不到任何解决方案。 所以我有 Alamofire 请求并且在处理数据类型时遇到了麻烦。我有那么多'printing out'只是为了一步步查看我得到了什么数据
Alamofire.request(URL, method: .get, headers: headers).responseJSON { response in
switch responseJSON.result {
case .success(let value):
print(type(of: value)) //__NSDictionaryI
print(value)
print(type(of:responseJSON)) //DataResponse<Any>
print(responseJSON) . //SUCCESS: {"billing_addresses" = (...
print(responseJSON.value as Any) . //Optional({...
//print(responseJSON.value as! [[String:Any]]) . //Could not cast value of type '__NSDictionaryI' (0x10b9fb508) to 'NSArray' (0x10b9fb008).
do {
let decoder = JSONDecoder()
let model = try decoder.decode(Info.self, from: value as! Data) //Decode JSON Response Data
print(model.id)
} catch let parsingError {
print("Error", parsingError)
}
现在我有一个错误:**Could not cast value of type '__NSSingleEntryDictionaryI' (0x10d240f78) to 'NSData' (0x10d241090).**
responseJSON 的值为:
(我不确定这个值是否正确,因为当我检查 Postman 时所有字符串都是双引号,并且 "is_default" 的值是 true/false,而不是 0/1。但是在 Xcode 中,我在控制台中得到了这个。所以 responseJSON 中可能有问题?..)
地址可能是零个,也可能是多个。
{
"id": 40128,
"username": "test6",
"email": "test6@on.com",
"billing_addresses": [
{
"address_name": null,
"country_code": "US",
"first_name": "Ted",
"last_name": "Qqqq",
"company_name": "",
"address_line1": "308 Sea Lane",
"address_line2": "",
"city": "QQQQ",
"state": "FL",
"postcode": "32000",
"email_address": "test6@on.com",
"phone_number": "11111111",
"is_default_for_billing": true
}
],
"shipping_addresses": [
{
"address_name": null,
"country_code": "US",
"first_name": "Ted",
"last_name": "Qqqq",
"company_name": "",
"address_line1": "308 Sea Lane",
"address_line2": "",
"city": "QQQQ",
"state": "FL",
"postcode": "32000",
"is_default_for_shipping": true
}
]
}
这是模型
struct Info : Decodable {
let id: Int
let email: String
let username: String
let billing_addresses: Billings
let shipping_addresses: Shippings
}
struct Billings: Decodable{
let address_name: String
let country_code: String
let first_name: String
let last_name: String
let company_name: String
let address_line1: String
let address_line2: String
let city: String
let state: String
let postcode: String
let email_address: String
let phone_number: String
let is_default_for_billing: Bool
}
struct Shippings:Decodable{
let address_name: String
let country_code: String
let first_name: String
let last_name: String
let company_name: String
let address_line1: String
let address_line2: String
let city: String
let state: String
let postcode: String
let is_default_for_shipping: Bool
}
如果我尝试使用带有 value
作为参数的 SwiftyJSON,我会遇到一个错误,即 Any
不能是 Data
,我真的不知道该怎么办。
responseJSON.result.value
returns 反序列化的集合类型,在你的例子中是字典 [String:Any]
要使用 JSONDecoder
,您需要 response.data
let model = try decoder.decode(Info.self, from: response.data) //Decode JSON Response Data
考虑到你会 运行 解码错误:billing_addresses
和 shipping_addresses
是数组
let billing_addresses: [Billings]
let shipping_addresses: [Shippings] // better name both structs in singular form (Billing, Shipping)
一些值可以是数字而不是字符串。
无论如何建议使用convertFromSnakeCase
密钥解码策略来摆脱难看的snake_case名字。
编辑:
这是带有驼峰命名和单数形式的结构,您必须添加
decoder.keyDecodingStrategy = .convertFromSnakeCase
struct Info : Decodable {
let id: Int
let email: String
let username: String
let billingAddresses: [Billing]
let shippingAddresses: [Shipping]
}
struct Billing : Decodable {
let addressName: String?
let countryCode, firstName, lastName, companyName: String
let addressLine1, addressLine2, city, state, postcode: String
let emailAddress, phoneNumber: String
let isDefaultForBilling: Bool
}
struct Shipping : Decodable {
let addressName: String?
let countryCode, firstName, lastName, companyName: String
let addressLine1, addressLine2, city, state, postcode: String
let isDefaultForShipping: Bool
}