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.
The result of the deserialization will be an array:
let data = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
do {
let dataArray = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSArray
for event in dataArray {
print(event)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.