2017-05-10 4 views
-2

Mit diesem Code i das "Ergebnis" zeigenLesen JSON-Datei mit Swift 3

var movies: [NSDictionary]? 
var dates: NSDictionary? 

func fetchNowPlayingMovies(){ 
     let apiKey = "8e3967947a95555f9c430e68d070a0e7" 
     let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)"+"&language=en-US&page=1") 
     let request = URLRequest(url: url! as URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 10) 
     let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main) 
     let task: URLSessionDataTask = session.dataTask(with:request, completionHandler: {(dataOrNil, reponse, error) in 
      if let data = dataOrNil{ 
       if let responseDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary { 
        //print("reponse: \(responseDictionary)") 
        self.movies = responseDictionary["results"] as? [NSDictionary] 
        self.dates = responseDictionary["dates"] as? NSDictionary 
        self.tableView.reloadData() 
       } 
       DispatchQueue.main.async { 
        self.tableView.reloadData() 
       } 
      } 
     }) 
     task.resume() 
    } 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! MovieTableViewCell 

     //MARK: - JSON Properties 
     let movie = movies![indexPath.row] 
     let date = dates![indexPath.row] 

     let title = movie["title"] as! String 
     let vote_average = movie["vote_average"] as! Double 
     let overview = movie["overview"] as! String 

     let baseUrl = "http://image.tmdb.org/t/p/w500" 
     let posterPath = movie["poster_path"] as! String 

     let imageUrl = URL(string: baseUrl + posterPath) 

     // Configure the cell... 
     cell.lblTitle.text = title 

     cell.lblOverview.text = overview 

     cell.lblVoteAVG.text = String(vote_average) 
     cell.imgPosterPath.sd_setImage(with: imageUrl) 

     return cell 
    } 

Anfrage: https://api.themoviedb.org/3/movie/now_playing?api_key=8e3967947a95555f9c430e68d070a0e7&language=en-US&page=1 , wie ich die "Seite" erhalten und das "Maximum" in "Termine"? Vielen Dank!

+1

half nicht Screenshot von Code schreiben Sie. Real Code kopieren/einfügen. Vermeiden Sie 'NSDictionary' in Swift3, bevorzugt Swifty Types Dictionary. Seite ist auf der gleichen Ebene wie Ergebnisse, also responseDictionary ["Seite"]? Gleiches gilt für die Daten. – Larme

+0

Ich bin ein Neuling, also verstehe ich nicht sehr gut. Kannst du das mit Code erklären? Vielen Dank! – NAK

Antwort

0
var movies: [NSDictionary]? 
var dates: NSDictionary? 

var page: Int? // var for page 
var maximum: String? // var for maximum in dates 

func fetchNowPlayingMovies(){ 
    let apiKey = "8e3967947a95555f9c430e68d070a0e7" 
    let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)"+"&language=en-US&page=1") 
    let request = URLRequest(url: url! as URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 10) 
    let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main) 
    let task: URLSessionDataTask = session.dataTask(with:request, completionHandler: {(dataOrNil, reponse, error) in 
     if let data = dataOrNil{ 
      if let responseDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary { 
       //print("reponse: \(responseDictionary)") 
       self.movies = responseDictionary["results"] as? [NSDictionary] 
       self.dates = responseDictionary["dates"] as? NSDictionary 

       // This is how you get to page 
       print("page: \(responseDictionary["page"]!)") 
       self.page = responseDictionary["page"] as? Int 
       print("page: \(self.page!)") 

       // This is how you get to maximum in dates 
       self.maximum = self.dates?["maximum"] as? String 
       print("maximum: \(self.maximum!)") 

       self.tableView.reloadData() 
      } 
      DispatchQueue.main.async { 
       self.tableView.reloadData() 
      } 
     } 
    }) 
    task.resume() 
} 

Ich hoffe, es

+0

Vielen Dank! – NAK

+0

Ich bin froh, dass ich helfen konnte. Bitte stimme bis zu meiner Antwort ab und klicke auf das Häkchen neben der Antwort, wenn es dir geholfen hat. [Was soll ich tun, wenn jemand meine Frage beantwortet?] (Http://stackoverflow.com/help/someone-answers) –