我正在从在线API中获取一些JSON,并将结果放入数组中以备将来使用。到目前为止,所有数据都很好(只是字符串数组(,但我无法弄清楚如何处理其中一个结果。
这是JSON(有人建议我使用 https://jsonlint.com 使其可读,这非常有用(
这是获取 JSON 的函数:
func getJSON(completionHandler: @escaping (Bool) -> ()) {
let jsonUrlString = "https://api.nytimes.com/svc/topstories/v1/business.json?api-key=f4bf2ee721031a344b84b0449cfdb589:1:73741808"
guard let url = URL(string: jsonUrlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data, err == nil else {
print(err!)
return
}
do {
let response = try
JSONDecoder().decode(TopStoriesResponse.self, from: data)
// Pass results into arrays (title, abstract, url, image)
for result in response.results {
let headlines = result.title
let abstracts = result.abstract
let url = result.url
self.headlines.append(headlines)
self.abstracts.append(abstracts)
self.urls.append(url)
}
let imageResponse = try
JSONDecoder().decode(Story.self, from: data)
for imageResults in imageResponse.multimedia {
let images = imageResults.url
self.images.append(images)
}
completionHandler(true)
} catch let jsonErr {
print("Error serializing JSON", jsonErr)
}
}.resume()
}
以下是序列化 JSON 的结构:
struct TopStoriesResponse: Decodable {
let status: String
let results: [Story]
}
struct Story: Decodable {
let title: String
let abstract: String
let url: String
let multimedia: [Multimedia]
}
struct Multimedia: Codable {
let url: String
let type: String
}
我将结果组织到这些数组中:
var headlines = [String]()
var abstracts = [String]()
var urls = [String]()
var images = [String]()
我在viewDidLoad
中调用该函数
getJSON { (true) in
print("Success")
print("nnHeadlines: (self.headlines)nnAbstracts: (self.abstracts)nnURLS: (self.urls)nnImages: (self.images)")
}
正如您在getJSON
函数中看到的那样,我尝试使用
let imageResponse = try JSONDecoder().decode(Story.self, from: data)
for imageResults in imageResponse.multimedia {
let images = imageResults.url
self.images.append(images)
}
但是我得到错误
CodingKeys(stringValue: "multimedia", intValue: nil(], debugDescription: "预期解码数组,但找到一个字符串/数据。
我很困惑,因为它说它期待一个数组,但找到了一个字符串 - 图像不是数组,就像headlines
、abstracts
等一样吗?
问题是multimedia
要么是Multimedia
对象的数组,要么是空的String
。您需要为Story
编写一个自定义初始化器来处理这个问题。
struct Story: Decodable {
let title: String
let abstract: String
let url: String
let multimedia: [Multimedia]
private enum CodingKeys: String, CodingKey {
case title
case abstract
case url
case multimedia
}
init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decode(String.self, forKey: .title)
abstract = try container.decode(String.self, forKey: .abstract)
url = try container.decode(String.self, forKey: .url)
multimedia = (try? container.decode([Multimedia].self, forKey: .multimedia)) ?? []
}
}
你multimedia
定义为数组。 该 json 中有一些部分没有任何多媒体,该多媒体设置为空字符串:
multimedia: ""
您需要能够处理这两种情况。 由于Codable
旨在处理具体类型,因此最好改用JSONSerialization
。
如果您强烈喜欢使用Codable
,则可以以字符串形式操作 JSON 响应,将multimedia: ""
转换为所需的格式,然后将其传递给解码器。例如,您可以将多媒体设置为可选,只需删除带有multimedia: ""
的任何行。
问题在于来自服务器的 JSON 响应,Multimedia
数组在其中一个 JSON 响应中以空"" String
的形式出现。要处理这种情况,您需要手动实现init(from decoder:)
方法并处理空的 String 情况。此外,您不需要创建单独的数组来存储值,您可以直接在完成处理程序闭包中传递TopStoriesResponse
结构,并在需要时在 ViewController 中获取值。
假设您创建了一个具有成功(T(和失败(错误(的结果枚举,并将其传递给completionHandler以供ViewController处理
enum Result<T> {
case success(T)
case failure(Error)
}
struct Networking {
static func getJson(completionHandler: @escaping (Result<TopStoriesResponse>) -> ()) {
let jsonUrlString = "https://api.nytimes.com/svc/topstories/v1/business.json?api-key=f4bf2ee721031a344b84b0449cfdb589:1:73741808"
guard let url = URL(string: jsonUrlString) else {
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data, error == nil else {
completionHandler(Result.failure(error!))
return
}
do {
let topStoriesResponse: TopStoriesResponse = try JSONDecoder().decode(TopStoriesResponse.self, from: data)
print(topStoriesResponse.results.count)
completionHandler(Result.success(topStoriesResponse))
} catch {
completionHandler(Result.failure(error))
}
}.resume()
}
}
现在,在您的视图控制器中,您可以调用getJson方法并在completionHandler中切换结果枚举以获取值
class ViewController: UIViewController {
var topStories: TopStoriesResponse?
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
func loadData() {
Networking.getJson { (result: Result<TopStoriesResponse>) in
switch result {
case let .success(topStories):
self.topStories = topStories
topStories.results.forEach({ (story: Story) in
print("Title: (story.title) n Abstracts = (story.abstract) URL = (story.url)")
})
//reload tableView
case let .failure(error):
print(error.localizedDescription)
}
}
}
}
要处理空的字符串大小写,您需要在多媒体结构中实现 init(解码器:( 方法,如上所述
struct Multimedia: Decodable {
let url: String
let image: String
let height: Float
let width: Float
}
struct Story: Decodable {
let title: String
let abstract: String
let url: String
let multimedia: [Multimedia]
private enum CodingKeys: String, CodingKey {
case title
case abstract
case url
case multimedia
}
init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decode(String.self, forKey: .title)
abstract = try container.decode(String.self, forKey: .abstract)
url = try container.decode(String.self, forKey: .url)
multimedia = (try? container.decode([Multimedia].self, forKey: .multimedia)) ?? []
}
}
struct TopStoriesResponse: Decodable {
let status: String
let copyright: String
let num_results: Int
let results: [Story]
}