Télécharger et parsingr json dans swift

J’essaie d’obtenir le JSON d’un site Web et de l’parsingr avant de le placer dans une vue iOS.

Voici mon code;

func startConnection(){ let urlPath: Ssortingng = "http://binaenaleyh.net/dusor/" var url: NSURL = NSURL(ssortingng: 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 } 

Et c’est le lien pour le JSON;

 http://binaenaleyh.net/dusor/ 

Que fais-je mal ici?

Ces deux fonctions ont fonctionné pour moi:

  func getJSON(urlToRequest: Ssortingng) -> NSData{ return NSData(contentsOfURL: NSURL(ssortingng: urlToRequest)) } func parseJSON(inputData: NSData) -> NSDictionary{ var error: NSError? var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary return boardsDictionary } 

Ce code fonctionne bien pour moi. Il suffit d’initialiser la propriété de data = NSMutableData() avec data = NSMutableData() et d’écrire ici class ViewController: UIViewController, NSURLConnectionDelegate

 import UIKit class ViewController: UIViewController, NSURLConnectionDelegate { @lazy var data = NSMutableData() override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) startConnection() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func startConnection(){ let urlPath: Ssortingng = "http://binaenaleyh.net/dusor/" var url: NSURL = NSURL(ssortingng: 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 println(jsonResult) } } 

voici la sortie:

 { "ders sayisi" = 15; dersler = ( { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 2; 12 = ""; 2 = 2; 3 = "CEE 102"; 4 = "Logic Circuits"; 5 = 3; 6 = "6.00"; 7 = "YRD.DO\U00c7.DR.INDRIT MYDERRIZI"; 8 = 0; 9 = IA; }, { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 1; 12 = ""; 2 = 2; 3 = "CIP 102"; 4 = "Civic Involment Projects"; 5 = 0; 6 = "2.00"; 7 = "SE\U00c7\U0130L AVCI/BA\U015eAK CANSU AK\U00c7EL\U0130K/\U00c7A\U011eLA UNGUN"; 8 = 0; 9 = P; }, { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 2; 12 = ""; 2 = 2; 3 = "COME 108"; 4 = "Algorithms and Programming II"; 5 = 3; 6 = "6.00"; 7 = "\U00d6\U011eR.G\U00d6R.DR.B\U0130RSEN G\U00dcLDEN \U00d6ZDEM\U0130R"; 8 = 41; 9 = C; }, { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 1; 12 = ""; 2 = 2; 3 = "COME 335"; 4 = "Mobile Application Development"; 5 = 3; 6 = "5.00"; 7 = "\U00d6\U011eR.G\U00d6R.OZAN UYSAL"; 8 = TeacherHold; 9 = TeacherHold; }, { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 1; 12 = ""; 2 = 2; 3 = "ENG 112"; 4 = "Advanced English For Engineering and Natural Sciences I"; 5 = 2; 6 = "3.00"; 7 = "OKT.ERIC BEECHER"; 8 = 48; 9 = F; }, { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 1; 12 = ""; 2 = 2; 3 = "PHYS 102"; 4 = "Physics II"; 5 = 4; 6 = "5.00"; 7 = "YRD.DO\U00c7.DR.\U00d6ZG\U00dcL KURTULU\U015e \U00d6ZT\U00dcRK"; 8 = "-1"; 9 = F; }, { 0 = "2013-2014"; 1 = BAHAR; 10 = TeacherHold; 11 = 2; 12 = ""; 2 = 2; 3 = "T\U00dcRK 102"; 4 = "T\U00fcrk Dili II"; 5 = 2; 6 = "2.00"; 7 = "\U00d6\U011eR.G\U00d6R.\U015eER\U0130FE GEZG\U0130N"; 8 = 10; 9 = F; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 2; 12 = ""; 2 = 1; 3 = "CHEM 101"; 4 = Chemistry; 5 = 3; 6 = "5.00"; 7 = "YRD.DO\U00c7.DR.AY\U015eEN TULPAR"; 8 = TeacherHold; 9 = F; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 1; 12 = ""; 2 = 1; 3 = "CIP 101"; 4 = "Civic Involment Projects"; 5 = 0; 6 = "1.00"; 7 = "YRD.DO\U00c7.DR.FATMA G\U00dcL AYGEN/Staff CIP1/SE\U00c7\U0130L AVCI/D\U0130LAYDA EM\U0130R/BA\U015eAK CANSU AK\U00c7EL\U0130K/\U00c7A\U011eLA UNGUN"; 8 = TeacherHold; 9 = P; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 3; 12 = ""; 2 = 1; 3 = "COME 107"; 4 = "Algorithms and Programming I"; 5 = 4; 6 = "5.00"; 7 = "PROF.DR.M\U0130TAT UYSAL"; 8 = TeacherHold; 9 = "C+"; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 2; 12 = ""; 2 = 1; 3 = "ENG 101"; 4 = "Advanced English"; 5 = 2; 6 = "3.00"; 7 = "OKT.EZG\U0130 ARGUN"; 8 = TeacherHold; 9 = B; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 1; 12 = ""; 2 = 1; 3 = "IUL 100"; 4 = "Introduction to University Life"; 5 = 0; 6 = "1.00"; 7 = "YRD.DO\U00c7.DR.FATMA G\U00dcL AYGEN"; 8 = TeacherHold; 9 = F; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 2; 12 = ""; 2 = 1; 3 = "MATH 111"; 4 = "Calculus I"; 5 = 4; 6 = "5.00"; 7 = "DO\U00c7.DR.G\U00dcRSEL YE\U015e\U0130LOT"; 8 = TeacherHold; 9 = F; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 4; 12 = ""; 2 = 1; 3 = "PHYS 101"; 4 = "Physics I [CNE]"; 5 = 4; 6 = "5.00"; 7 = "YRD.DO\U00c7.DR.AR\U0130F \U00d6ZBAY"; 8 = TeacherHold; 9 = F; }, { 0 = "2013-2014"; 1 = "G\U00dcZ"; 10 = TeacherHold; 11 = 9; 12 = ""; 2 = 1; 3 = "T\U00dcRK 101"; 4 = "T\U00fcrk Dili I [CNE]"; 5 = 2; 6 = "2.00"; 7 = "\U00d6\U011eR.G\U00d6R.ARZU AYG\U00dcN"; 8 = TeacherHold; 9 = F; } ); } 

MISE À JOUR: Swift 4

 // Asynchronous Http call to your api url, using URLSession: URLSession.shared.dataTask(with: URL(ssortingng: "http://api.site.com/json")!) { (data, response, error) -> Void in // Check if data was received successfully if error == nil && data != nil { do { // Convert to dictionary where keys are of type Ssortingng, and values are of any type let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [Ssortingng: Any] // Access specific key with value of type Ssortingng let str = json["key"] as! Ssortingng } catch { // Something went wrong } } }.resume() 

Voici comment procéder avec Swift 2 et NSURLSession:

 // Asynchronous Http call to your api url, using NSURLSession: NSURLSession.sharedSession().dataTaskWithURL(NSURL(ssortingng: "http://api.site.com/json")!, 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 Ssortingng, and values are of any type let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [Ssortingng:AnyObject] // Access specific key with value of type Ssortingng let str = json["key"] as! Ssortingng } catch { // Something went wrong } } }).resume() 

Si le JSON d’origine est un tableau, essayez ceci.

 func parseJSON(inputData: NSData) -> Array{ var error: NSError? var boardsDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as Array return boardsDictionary } 

L’exemple ci-dessous envoie une demande pour récupérer des informations boursières – c’est un peu plus complet, mais a plus de nettoyage et de structure:

 import Foundation private let kSNStockInfoFetchRequestPath:Ssortingng = "http://dev.markitondemand.com/Api/v2/Quote/json" private func SNStockInfoFetchRequestURL(symbol:Ssortingng) -> NSURL? { if let components:NSURLComponents = NSURLComponents(ssortingng:kSNStockInfoFetchRequestPath) { components.queryItems = [NSURLQueryItem(name:"symbol", value:symbol)] return components.URL } return nil } private func SNStockInfoFetchRequestURLRequest(symbol:Ssortingng) -> NSURLRequest? { if let requestURL:NSURL = SNStockInfoFetchRequestURL(symbol) { var request:NSMutableURLRequest = NSMutableURLRequest(URL:requestURL) request.HTTPMethod = "GET" return request } return nil } private func SNStockInfoFetchRequestParseData(receivedData:NSData, error:NSErrorPointer) -> NSDictionary? { return NSJSONSerialization.JSONObjectWithData(receivedData, options:NSJSONReadingOptions.MutableContainers, error:error) as? NSDictionary } class SNStockInfoFetchRequest: NSObject, NSURLConnectionDataDelegate { private let symbol:Ssortingng private (set) var fetching:Bool private lazy var receivedData = NSMutableData() init(symbol:Ssortingng) { self.symbol = symbol self.fetching = false } func start() { assert(!fetching, "Should not start a request that has already started!") fetching = true if let request:NSURLRequest = SNStockInfoFetchRequestURLRequest(symbol) { var connection:NSURLConnection = NSURLConnection(request:request, delegate:self, startImmediately:true)! connection.start() } } // MARK: NSURLConnectionDataDelegate func connection(connection:NSURLConnection, didReceiveData data:NSData) { assert(fetching, "Should only receive data while activly fetching!") self.receivedData.appendData(data) } func connectionDidFinishLoading(connection:NSURLConnection) { var error:NSError? if let result:NSDictionary = SNStockInfoFetchRequestParseData(receivedData, &error) { println(result) } else { println(error) } } } 

Nous pouvons maintenant utiliser la demande en tant que telle:

 let fetcher:SNStockInfoFetchRequest = SNStockInfoFetchRequest(symbol:"MSFT") fetcher.start() 

Cela devrait générer le JSON en cas de succès ou l’erreur en cas de défaillance. J’espère que cela t’aides! Si vous utilisez la même structure, veillez à utiliser un délégué pour renvoyer le JSON analysé (ou même mieux, un object de valeur immuable) ou pour indiquer un échec.

La réponse de @ david72 a parfaitement fonctionné pour moi. Alors, voici la même chose dans Swift 3 et URLSession pour votre commodité. Également avec une impression d’erreur supplémentaire pour un débogage plus facile.

 func getHttpData(urlAddress : Ssortingng) { // Asynchronous Http call to your api url, using NSURLSession: guard let url = URL(ssortingng: urlAddress) else { print("Url conversion issue.") return } URLSession.shared.dataTask(with: 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 Ssortingng, and values are of any type let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [Ssortingng:AnyObject] print(json) // Access specific key with value of type Ssortingng // let str = json["key"] as! Ssortingng } catch { print(error) // Something went wrong } } else if error != nil { print(error) } }).resume() } 

Voici le code 3.1 rapide

 URLSession.shared.dataTask(with: NSURL(ssortingng: "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 Ssortingng, and values are of any type let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [Ssortingng:AnyObject] // Access specific key with value of type Ssortingng let str = json["key"] as! Ssortingng } catch { // Something went wrong } } }).resume() 
 // // ViewController.swift // Test2 // // Created by fingent on 11/08/15. // Copyright (c) 2015 fingent. All rights reserved. // import UIKit class ViewController: UIViewController,NSURLConnectionDelegate { lazy var data = NSMutableData() @IBAction func t1(sender: AnyObject) { } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. startConnection() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func startConnection(){ let urlPath: Ssortingng = "https://api.github.com/users/mralexgray" var url: NSURL = NSURL(ssortingng: 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 println(jsonResult) } }