我正在尝试解析以下 JSON:
{
"String For Key 1": [
"Some String A",
"Some String B",
"Some String C",
],
"String For Key 2": [
"Some String D",
"Some String E",
"Some String F"
],
"String For Key 3": [
"Some String G",
"Some String H",
"Some String I",
],
"String For Key 4": [
"Some String J",
"Some String K",
"Some String L"
],
"String For Key 5": [
"Some String M",
"Some String N",
"Some String O"
],
"String For Key 6": [
"Some String P",
"Some String Q",
"Some String R"
]
}
我也在关注本教程。它在 Playground 的 Github 上
在示例中,他们正在Array
使用typealias JSONDictionary = [String: AnyObject]
我需要解析一个Dictionary
具有Key
哪个是的String
,Value
那是一个Array
。因此:typealias JSONDictionary = [String: [AnyObject]]
并且在返回flatMap
.
extension Resource {
init(url: NSURL, parseJSON: AnyObject -> A?) {
self.url = url
self.parse = { data in
let json = try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: [AnyObject]]
//This is where I get an error
return json.flatMap(parseJSON)
}
}
}
错误:
Cannot convert value of type `AnyObject -> A? to expected argument type `([String: [AnyObject]]?) -> _?
如何获得带有Keys
asString
和Values
as an的字典Array
?我想保持他们的Generic
做法。