• Swift 3 JSON

    iOS 10 ile beraber gelen Swift 3 bazı değişiklere sahip. Swift 2.3 de kullandığımız JSON kodlaması Swift 3.0 convert ettiğimizde hata verecektir.

    Bu yazımda size Swift 3.0 JSON veri okumayı anlatacağım.

    Aşağıdaki JSON verisini kullanacağız.

    ekran-resmi-2016-09-23-16-15-28

    KODLAMA :


    let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
    let session = URLSession.shared
    let task = session.dataTask(with: urlRequest as URLRequest) {
    (data, response, error) -> Void in

    let httpResponse = response as! HTTPURLResponse
    let statusCode = httpResponse.statusCode

    if (statusCode == 200) {
    print(“Everyone is fine, file downloaded successfully.”)

    do{

    let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]

    if let stations = json[“stations”] as? [[String: AnyObject]] {

    for station in stations {

    if let name = station[“stationName”] as? String {

    if let year = station[“buildYear”] as? String {
    print(name,year)
    }

    }
    }

    }

    }catch {
    print(“Error with Json: \(error)”)
    }

    }
    }

    task.resume()

     

    Categories: iOS Programlama

    Etiketler: , , , , ,

    Bir cevap yazın