i am using my REST Webservice to get the JSON Data i want, this works as intended and prints:
[{
"name": "Event1",
"genre": "Party",
"subtitle": "subtitle1",
"startDate": "2015-10-10",
"location": "Anywhere"
},
{
"name": "Event2",
"genre": "Party",
"subtitle": "subtitle2",
"startDate": "2015-10-10",
"location": "Anywhere"
}]
So this seems to be an Array with 2 Elements which are Dictionaries.
I then tried to parse the JSON with NSJSONSerialization.
let data = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
if let name = json["name"] as? [String]{
print(name)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
I do get this error:
Could not cast value of type '__NSCFArray' (0x1096c1ae0) to 'NSDictionary' (0x1096c1d60).
which seems pretty clear to me, but i just don't know how to solve it.
My goal would be to create "Event" Objects from my own class.
jsonis a array of dictionary. With thatjson["name"]you're assuming it's a dictionary, which is not. Sojson[0]['name"]?