Downloading and parsing json in swift
json, parsing, swift
Solution
These two functions worked for me:
func getJSON(urlToRequest: String) -> NSData{
return NSData(contentsOfURL: NSURL(string: urlToRequest))
}
func parseJSON(inputData: NSData) -> NSDictionary{
var error: NSError?
var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
return boardsDictionary
}
Problem
I'm trying to get the JSON from a website and parse it before putting it inside of an iOS view. Here's my code; ``` func startConnection(){ let urlPath: String = "http://binaenaleyh.net/dusor/" var url: NSURL = NSURL(string: urlPath) var request: NSURLRequest = NSURLRequest(URL: url) var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false) connection.start() } func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ self.data.appendData(data) } func buttonAction(sender: UIButton!){ startConnection() } func connectionDidFinishLoading(connection: NSURLConnection!) { var err: NSError // throwing an error on the line below (can't figure out where the error message is) var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary } ``` And this is the link for the JSON; ``` http://binaenaleyh.net/dusor/ ``` What am I doing wrong here?