gpt4 book ai didi

ios - swift 对象映射器 : How to parse array inside of an array

转载 作者:可可西里 更新时间:2023-11-01 01:07:54 25 4
gpt4 key购买 nike

这是我的 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 是 Apple 的一个非常酷的协议(protocol),用于处理来自 API 的 JSON 响应解析。你得到的是一个数组数组,所以你的东西看起来像这样:

[[ResponseObject]]

所以不管怎样,你会为你的对象创建一个结构,就像这样:

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

您会注意到我稍微更改了 key 名称(而不是 request_id,我使用了 requestId)。原因是 JSONDecoder 有一个名为 keyDecodingStrategy 的属性,它提供了一系列可供选择的固定解码策略。你会做 convertFromSnakeCase

您可以将以下代码转储到 Playground 中进行修补。基本上,声明您的结构,将其与 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)
}
}
}

这是您可以在 Playground 上使用的代码。

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)
}
}

关于ios - swift 对象映射器 : How to parse array inside of an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789481/

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