Downloading and parsing json in swift

前端 未结 9 1212
独厮守ぢ
独厮守ぢ 2020-12-02 05:37

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(){
        le         


        
9条回答
  •  旧时难觅i
    2020-12-02 06:12

    Here is swift 3.1 code

    URLSession.shared.dataTask(with: NSURL(string: "http://pastebin.com/raw/wgkJgazE")! as URL, completionHandler: { (data, response, error) -> Void in
            // Check if data was received successfully
            if error == nil && data != nil {
                do {
                    // Convert NSData to Dictionary where keys are of type String, and values are of any type
                    let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
                    // Access specific key with value of type String
                    let str = json["key"] as! String
                } catch {
                    // Something went wrong
                }
            }
        }).resume()
    

提交回复
热议问题