2017-10-11 6 views
0

Comment puis-je analyser ce JSON?swift analyser JSON à partir de l'URL

{ 
    "telefon":"05538690671" 
} 

mon code:

let url = NSURL(string: "http://localhost.192.168.1.40.xip.io:8888/store1.php") 

    //fetching the data from the url 
    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in 

     if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { 

      //printing the json in console 
      print(jsonObj!.value(forKey: "telefon")!) 

désolé pour mon anglais

Répondre

0

Je nettoyais les choses un peu, laisser tomber les choses NS et les résultats protégés par guard.

let url = URL(string: "http://localhost.192.168.1.40.xip.io:8888/store1.php")! 

URLSession.shared.dataTask(with: url) { data, _, _ in 
    guard let data = data else { return } 
    guard let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) else { return } 
    guard let jsonObj = json as? [String: Any] else { return } 

    guard let telefon = jsonObj["telefon"] as? String else { return } 

    print(telefon) 
} 

Un peu plus débogage.

let url = URL(string: "http://localhost.192.168.1.40.xip.io:8888/store1.php")! 

URLSession.shared.dataTask(with: url) { data, _, _ in 
    guard let data = data, !data.isEmpty else { 
     print("Error: data is nil or empty") 
     return 
    } 

    guard let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) else { 
     print("Error: data contains no JSON") 
     return 
    } 

    guard let jsonObj = json as? [String: Any] else { 
     print("Error: JSON is not a dictionary") 
     return 
    } 

    guard let telefon = jsonObj["telefon"] as? String else { 
     print("Error: Object has no 'telefon' key") 
     return 
    } 

    print(telefon) 
} 
+0

désolé, quand je suis tracer ce saut de code de dataTask à la fin du code –

+0

@ ÜmitGündüz Cela signifie une des conditions de 'guard' ont échoué. –

+0

@ ÜmitGündüz J'ai ajouté une nouvelle version avec plus de débogage dans certaines des conditions d'échec. –

0

Utilisez Alamofire (https://github.com/Alamofire/Alamofire) pour récupérer les données.

Ajoutez Alamofire à votre projet.

classe personnalisée pour analyser

import UIKit 
    import Alamofire 

    typealias DownloadComplete =() ->() 

    class YourClassName { 

    private var _telefon: String! 

    var telefon: String { 
      if _telefon == nil { 
       _telefon = "" 
      } 
      return _telefon 
     } 

    func downloadDetails(completed: @escaping DownloadComplete) { 
      let currentURL = URL(string: "http://localhost.192.168.1.40.xip.io:8888/store1.php")! 
      Alamofire.request(currentURL).responseJSON { response in 
       let result = response.result 
      if let dict = result.value as? Dictionary<String, AnyObject> { 
       if let telefon = dict["telefon"] as? String { 
       self._telefon = telefon 
       } 
      } 
      } 
     completed() 
    } 
}