Swift ObjectMapper:如何解析数组内部的数组



这是我的JSON响应:

[
[
{
"id": 22,
"request_id": "rqst5c12fc9e856ae1.06631647",
"business_name": "Code Viable",
"business_email": "code@viable.com",
"title": "Apache Load/Ubuntu",
}
],
[
{
"id": 24,
"request_id": "rqst5c130cae6f7609.41056231",
"business_name": "Code Viable",
"business_email": "code@viable.com",
"title": "Load",
}
]
]

这个JSON结构在数组中获得了一个数组,内部数组的对象就是我试图解析的对象。这是我的地图:

struct JobResponseDataObject: Mappable {
init?(map: Map) {
}
var id: Int?
var requestId: String?
var businessName: String?
var businessEmail: String?
mutating func mapping(map: Map) {
id              <- map["id"]
requestId       <- map["request_id"]
businessName    <- map["business_name"]
businessEmail   <- map["business_email"]
}
}

我尝试创建另一个映射器struct来保存对象[JobResponseDataObject]的数组,并使用Alamofire的responseArray,但没有成功。我也尝试过用0.作为json id的前缀,但这也不起作用。请帮助

感谢

所以交易是这样的。。。Codable是来自苹果的一个非常酷的协议,用于处理来自API的JSON响应解析。你得到的是一个数组,所以你的东西会是这样的:

[[ResponseObject]]

所以不管怎样,你应该为你的对象做一个结构,就像这样:

struct ResponseObject: Codable {
let id: Int?
let requestId: String?
let businessName: String?
let businessEmail: String?
let title: String?
}

您会注意到我稍微更改了密钥名称(我使用的不是request_id,而是requestId(。原因是JSONDecoder有一个名为keyDecodingStrategy的属性,它提供了一个可以从中选择的罐装解码策略的枚举。你会做convertFromSnakeCase

这里有一些代码,你可以把它们扔到操场上修改。基本上,声明您的结构,将其与JSON中的任何密钥匹配,声明解码器,向其提供解码策略,然后对其进行解码

以下是如何拨打Alamofire电话:

private let backgroundThread = DispatchQueue(label: "background",
qos: .userInitiated,
attributes: .concurrent,
autoreleaseFrequency: .inherit,
target: nil)

Alamofire.request(url).responseJSON(queue: backgroundThread) { (response) in
guard response.result.error == nil else {
print("💥KABOOM!💥")
return
}
if let data = response.data {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let parsedResponse = try decoder.decode([[ResponseObject]].self, from: data)
print(parsedResponse)
} catch {
print(error.localizedDescription)
}
}
}

这是你可以在操场上扔的代码。

import UIKit
let json = """
[
[
{
"id": 22,
"request_id": "rqst5c12fc9e856ae1.06631647",
"business_name": "Code Viable",
"business_email": "code@viable.com",
"title": "Apache Load/Ubuntu",
}
],
[
{
"id": 24,
"request_id": "rqst5c130cae6f7609.41056231",
"business_name": "Code Viable",
"business_email": "code@viable.com",
"title": "Load",
}
]
]
"""
struct ResponseObject: Codable {
let id: Int?
let requestId: String?
let businessName: String?
let businessEmail: String?
let title: String?
}
if let data = json.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let parsedResponse = try decoder.decode([[ResponseObject]].self, from: data)
print(parsedResponse)
} catch {
print(error.localizedDescription)
}
}

您应该将此JobResponseDataObject结构用作[[JobResponseDataObject]],而不是[JobResponseDataObject]——在父结构或类中使用此结构生成属性。

您可以在这里使用Codable来映射JSON响应,JobResponseDataObjectstruct应该看起来像,

struct JobResponseDataObject: Codable {
var id: Int?
var requestId: String?
var businessName: String?
var businessEmail: String?
var title: String?
private enum CodingKeys: String, CodingKey {
case id = "id"
case requestId = "request_id"
case businessName = "business_name"
case businessEmail = "business_email"
case title = "title"
}
}
let json = JSON(responseJSON: jsonData)
do {
if let value = try? json.rawData(){
let response = try! JSONDecoder().decode([[JobResponseDataObject]].self, from: value)
}
} catch {
print(error.localizedDescription)
}

相关内容

  • 没有找到相关文章

最新更新