2017-07-19 8 views
1

私は自分のサイトからデータを取得しようとしていますが、Alamofireを使用していますが、そのデータを配列に入れてテーブルビューを作成できますか?Alamofireを使用している場合、配列にJSONデータを配置

override func viewDidLoad() { 
     super.viewDidLoad() 

     Alamofire.request("http://lytestech.ga/api/lytes/get_movies/").responseJSON { response in 


      if let json = response.result.value { 
       print("JSON: \(json)") // serialized json response 
       let res = json as! NSDictionary 
       let movies = res["movies"] as! NSArray 
       // movieTitles = movies["movie_desc"] 
       let movieTitles: [String] = movies["movietitile"] as! String 
       print (movies) 
       print (movieTitles) 

      } 


      if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) { 
       print("Data: \(utf8Text)") // original server data as UTF8 string 
      } 
     } 

    } 

JSONデータ

JSON: { 
    movies =  (
       { 
      id = 66; 
      "movie_desc" = "spiders bite"; 
      movietitile = spiderman; 
     }, 
       { 
      id = 64; 
      "movie_desc" = horror; 
      movietitile = mummy; 
     } 
    ); 
    status = ok; 
} 
(
     { 
     id = 66; 
     "movie_desc" = "spiders bite"; 
     movietitile = spiderman; 
    }, 
     { 
     id = 64; 
     "movie_desc" = horror; 
     movietitile = mummy; 
    } 
) 
+0

あたりとして使用することができます。 com/SwiftyJSON/SwiftyJSON –

+0

https://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift?rq=1 –

+0

あなたはテーブルビューを作成する方法を知っていますかアレイから? – raki

答えて

0

あなたArrayDictionaryから、そこにあるキー名抽出あなたの応答Data

let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: response.data) as! [String : Any] 

Dictionaryに変換movies

if let arryMovies1 = dictionary["movies"] as? [[String:Any]] { 

     print (arryMovies1); 

    // Now your have your Array 
    // You can Populate into UItableView 
    // when your array is modified than you have to reload the tableData 


     self.arryMovies=arryMovies1 
     self.tableView.reloadData() 

     } 
0リストビューに取り込むためのあなたの cellForRowAtIndexPath

私は、これはあなたができるcellForRowAt方法で映画の中で、あなたにその後NSArray

var movies = NSArray() 

0
var movies:[Dictionary<String,AnyObject>] = [] // Makes movies array global 
movies = res["movies"] // and tableview reload 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let identifier = "cell" 
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ?? 
     UITableViewCell(style: .default, reuseIdentifier: identifier) 

    cell.textLabel!.text = movies[indexPath.row]["movietitile"] as? String 

    return cell 
} 
0

ストアの映画を助けることを願っています

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

       let dicMovie = self.arryMovies[indexPath.row] as! NSDictionary 

       cell.lableTitle.text = dicMovie.value(forKey: "movietitile") as? String 

      return cell 
     } 

このようなデータを表示する。

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

      let movieDetail = movies[indexPath.row] as! NSDictionary 

      cell.lblMovieName.text = movieDetail.value(forKey: "movietitile") as? String 
      return cell 
     } 

注: - :https://でgithubの。これは、単にコードスケルトンで、あなたはこのためにSwiftyJSONを使用することができますカスタムセルやアウトレット

0

enter image description here

func demo() 
{ 
    let str : String = "http://lytestech.ga/api/lytes/get_movies/" 
    let url = NSURL(string:str) 
    let request = NSMutableURLRequest(URL: url!) 
    request.HTTPMethod = "POST" 
    request.setValue("application/json", forHTTPHeaderField: "Content-Type") 
    Alamofire.request(request) 
     .responseString { response in 
      switch (response.result) { 
      case .Success(let JSON): 
       let data = JSON.dataUsingEncoding(NSUTF8StringEncoding)! 
       do { 
        let responseString = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject] 
        // print(responseString!) 
        self.movieName = [] 
        self.desc = [] 
        let arr = responseString!["movies"] as! NSArray 
        for j in 0..<arr.count{ 
         let dic = arr[j] as! NSMutableDictionary 
         self.movieName.addObject(dic.valueForKey("movietitile")!) 
         self.desc.addObject(dic.valueForKey("movie_desc")!) 
        } 
        self.removeIndicator() 
        self.contactTable.reloadData() 
       } catch _ as NSError { 
        //print(error) 
       } 
       break 
      case .Failure: 
       print("FAILURE") 
       self.removeIndicator() 
       break 
      } 
    } 
} 
関連する問題