I am trying to parse the following 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"
]
}
I am also following this tutorial. It's on Github in Playground
In the example, they are parsing an Array
of dictionaries using typealias JSONDictionary = [String: AnyObject]
I need to parse a Dictionary
that has a Key
which is String
, and Value
that is an Array
. Hence: typealias JSONDictionary = [String: [AnyObject]]
and I am getting an error when returning a 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)
}
}
}
Error:
Cannot convert value of type `AnyObject -> A? to expected argument type `([String: [AnyObject]]?) -> _?
How can I get a dictionary with Keys
as String
and Values
as an Array
? I would like to keep their Generic
approach.
keys
? How do I get the keys as akey
andvalue
asArray
? – Semirigid