gpt4 book ai didi

json - Swift .flatMap 未将 json 数据初始化为结构

转载 作者:行者123 更新时间:2023-11-30 11:25:15 25 4
gpt4 key购买 nike

我下载了一个 JSON 文件,我正在尝试将 .flatMap 转换为我创建的结构数组,但 .flatMap 并未将数据初始化到结构中。

下面是 JSON 文件的解析版本,它代表数组中的一个结构:

[["eventUri": <null>, 
"wgt": 266482320,
"source": {
dataType = news;
title = WAFB;
uri = "wafb.com";
},
"uri": 885629090,
"body": A bunch of text,
"lang": eng,
"dateTime": 2018-06-12T06:52:00Z,
"date": 2018-06-12,
"sim": 0,
"isDuplicate": 1,
"title": Now a candidate, Romney appears to embrace Trump presidency,
"time": 06:52:00,
"url": http://www.wafb.com/story/38391905/now-a-candidate-romney-appears-to-embrace-trump-presidency,
"dataType": news]]

这是结构体,带有结构体的共享实例,以及 flatMap 方法。所有初始化器下标均根据 JSON 的键命名:

struct Article {

static var sharedInstance: [Article] = [Article]()

// MARK: Properties

var dataType = ""
var date = ""
var eventUri = ""
var isDuplicate = false
var sourceName = ""
var lang = ""
var sim = 0.0
var time = ""
var articleTitle = ""
var image: UIImage? = nil

// MARK: Initializer
init?(dictionary: [String:AnyObject]) {

// GUARD: Do all dictionaries have values?
guard
let dataType = dictionary[EventRegistry.ParameterValues.dataType] as? String,
let date = dictionary[EventRegistry.ParameterValues.date] as? String,
let eventUri = dictionary[EventRegistry.ParameterValues.eventUri] as? String,
let isDuplicate = dictionary[EventRegistry.ParameterValues.isDuplicate] as? Bool,
let source = dictionary[EventRegistry.ParameterKeys.source] as? [String:Any],
let sourceName = source[EventRegistry.ParameterValues.sourceName] as? String,
let lang = dictionary[EventRegistry.ParameterValues.lang] as? String,
let sim = dictionary[EventRegistry.ParameterValues.sim] as? Double,
let time = dictionary[EventRegistry.ParameterValues.time] as? String,
let articleTitle = dictionary[EventRegistry.ParameterValues.articleTitle] as? String

// If not, return nil
else { return nil }

// Otherwise, initialize values
self.dataType = dataType
self.date = date
self.eventUri = eventUri
self.isDuplicate = isDuplicate
self.sourceName = sourceName
self.lang = lang
self.sim = sim
self.time = time
self.articleTitle = articleTitle
}

static func articlesFromResults(_ results: [[String:AnyObject]]) -> [Article] {

return results.flatMap(Article.init)
}

}

这是我对启动 flatMap 方法的调用,该方法不返回结果:

func getEventRegistrySearchResults(completionHandlerForSearchResults: @escaping (_ articles: [Article]?, _ errorString: String?) -> Void) {

taskForGetEventRegistrySearchResults(method: "", parameters: ["" : (Any).self]) { (result, error) in

if error != nil {
print("There was an error getting articles")
}

if let result = result {

if let articles = result["articles"] as? [String:AnyObject] {

if let articleResults = articles["results"] as? [[String:AnyObject]] {
Article.sharedInstance = Article.articlesFromResults(articleResults)
print(Article.sharedInstance.count) // Returns 0
}
} else {
completionHandlerForSearchResults(nil, "There was a problem")
}
}
}
}

在此处打印“articleResults”会为我提供上面的 JSON 文件,但是当我尝试将其 .flatMap 到我的结构数组中时,它不会初始化其中任何一个。我错过了什么?

最佳答案

如果我是你,我会在我的 Article 结构中实现 Swift 4 Codable 协议(protocol),然后使用 JSONDecoder 解码数组,如下所示

let decoder = JSONDecoder()
let articles = try! decoder.decode(Array<Article.self>, for: results)

所以你的文章结构将如下所示:

struct Article: Codable {
var dataType: String
var date: Date
var eventUri: URL
var isDuplicate: Bool
var sourceName: String
var lang: String
var sim: Double
var time: String
var articleTitle: String
var image: UIImage? = nil

static func articlesFromResults(_ results: [[String : Any]]) -> [Article] {
let decoder = JSONDecoder()
let articles = try! decoder.decode(Array<Article.self>, for: results)
}
}

现在,这留下了图像属性的问题,因为 UIImage 不符合 Codable,但您可以在此处查找解决方案 How to conform UIImage to Codable?

关于json - Swift .flatMap 未将 json 数据初始化为结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50811471/

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