gpt4 book ai didi

json - swift 对象映射器 : How to parse JSON with backslash

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

我已经在 stackoverflow 上尝试了几乎所有可能的解决方案,但到目前为止没有成功,

这是我的 json 响应:

[
"{\"id\":5,\"request_id\":\"rqst5c17fc752d44f1.15452158\",\"business_name\":\"611 Solutions\",\"business_email\":\"611thesolutions@gmail.com\",\"title\":\"123ABC - TESTING\",\"details\":\"Package is fragile, please haul with care\",\"load_description\":\"Royal Timber\",\"amount_offered\":\"2500\",\"pickup_address\":\"123 Colliumeal Dr, Fort Wayne, Indiana\",\"dropoff_address\":\"647 Airportway, Chicago, Illinois\",\"timestamp\":\"2018-12-17 19:43:49\"}"
]

注意 json 的键和值中有反斜杠,我的解析失败了,这就是我解析 json 的方式:

Alamofire.request(JOB_REQUEST_BASE_URL, method: .post, parameters: parameter, encoding: URLEncoding(), headers: nil).responseArray { (response: DataResponse<[JobResponseDataObject]>) in

log.debug("Fetching Job Requests...")

switch response.result {

case .success(let responseArray) :
log.debug(response.debugDescription)
log.debug("Sucessfully fetch job requests")
log.debug("Job request counts: \(responseArray.count)")
completionHandler(JobRequest.fetchJobRequest.Response(jobResponses: responseArray), nil)

case .failure(let error) :

log.debug("Fetching error: JobRequest")
log.debug(error.localizedDescription)
completionHandler(nil, .FailedToFetchEmptyJobRequests)

}
}

我还尝试使用 .responseString 获取纯字符串和做let json = response.result.value?.replacingOccurrences(of: "\\", with: "")并像这样映射它let jobs = Mapper<JobResponseDataObject>().map(JSONString: json!)到目前为止也没有运气。请帮忙

谢谢

最佳答案

你可以试试

if let str = responseArray.first as? String , let data = str.data(using:.utf8) {

do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let res = try decoder.decode(Root.self,from:data)
}
catch {
print(error)
}
}

struct Root: Codable {
let id: Int
let requestId, businessName, businessEmail, title: String
let details, loadDescription, amountOffered, pickupAddress: String
let dropoffAddress, timestamp: String
}

关于json - swift 对象映射器 : How to parse JSON with backslash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53823873/

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