I tried the following examples for parsing a JSON file (for example, the answer to another question posted here: https://stackoverflow.com/a/16729/ ) but cannot get it working. Now I get the error message "I canβt adjust the value of the type" AnyObject "to" let ... = item ["..."] as the string "String".
func connectionDidFinishLoading(connection: NSURLConnection) { do { let jsonResult = try NSJSONSerialization.JSONObjectWithData(self.bytes!, options: NSJSONReadingOptions.MutableContainers) as! Dictionary<String, AnyObject> if let searchResults = jsonResult["Search"] as? [AnyObject] { for item in searchResults { let title = item["Title"] as? String //Error Here let type = item["Type"] as? String //Error Here let year = item["Year"] as? String //Error Here print("Title: \(title) Type: \(type) Year: \(year)") } } } catch let error as NSError { NSLog("JSON Error: \(error)") } }
JSON example:
{ "Search": [ { "Title":"Example 1", "Year":"2001", "Type":"Type1" }, { "Title":"Example 2", "Year":"2006", "Type":"Type1" }, { "Title":"Example 3", "Year":"1955", "Type":"Type1" } ]}
json swift2
Daniel Oct 25 '15 at 0:17 2015-10-25 00:17
source share