2017-04-19 19 views
0

私は残りのWebサービスを消費しようとしている...解析JSON - Alamofire

私のJSON応答の構造は次のとおりです。

"page": 1, 
    "results": [ 
    { 
     "poster_path": "/9Hj2bqi955SvTa5zj7uZs6sic29.jpg", 
     "adult": false, 
     "overview": "", 
     "release_date": "2015-03-15", 
     "genre_ids": [ 
     99 
     ], 
     "id": 441580, 
     "original_title": "The Jinx: The Life and Deaths of Robert Durst Season 1 Chapter 6: What the Hell Did I Do?", 
     "original_language": "en", 
     "title": "The Jinx: The Life and Deaths of Robert Durst Season 1 Chapter 6: What the Hell Did I Do?", 
     "backdrop_path": "/3br0Rt90AkaqiwVBZVvVUYD1juQ.jpg", 
     "popularity": 1, 
     "vote_count": 1, 
     "video": false, 
     "vote_average": 10 
    } 
], 
    "total_results": 307211, 
    "total_pages": 15361 
} 

は、私がページと結果の配列を取得しようとしています...解析後はページ(paginationCount)と結果(jsonArray)変数はnilです。私のコードは

あります:あなたはポストの要求を持っている場合

Alamofire.request(ConstantHelper.kUrlDiscoverMovies, method: .get, parameters: ["api_key": ConstantHelper.kApiKey, "certification" : average, "sort_by" : "vote_average.desc" ]).validate() 
      .responseJSON { response in 
       switch response.result { 
       case .success: 
        if let repoJSON = response.result.value as? JSON { 
         let jsonArray = repoJSON["results"] as? NSMutableArray 
         for item in jsonArray! { 
          guard let movie = Movie(json: item as! JSON) else 
          { 
           print("Issue deserializing model") 
           return 
          } 
          listMovies.append(movie) 
         } 
         if let paginationCount = repoJSON["total_pages"] as? String { 
          completion(listMovies, Int(paginationCount)!, nil) 
         } 
         else { 
          completion(listMovies, 0, nil) 
         } 
        } 
        break 
       case .failure(let error): 
        completion(nil, 0, error as NSError?) 
        break 
       } 
     } 
+0

解析後ですか?どこ?成功の封鎖の中で、それは動作しますか? – Larme

+0

成功のクロージャ内... paginationCountをnilにする – Developer2012

答えて

3

私は、

でどのようなタイプJSON知らないが、キーresultsの値が[[String:Any]]で、決してNSMutableArray

let jsonArray = repoJSON["results"] as? [[String:Any]] 

とキーtotal_pagesの値がIntではありませんString(二重引用符はありません)

if let paginationCount = repoJSON["total_pages"] as? Int { 
    completion(listMovies, paginationCount, nil) 
} 
0

あなたは簡単にいくつかのネイティブ・スウィフト方法(alarmofireを使用する必要はありません)を使用してJSONを解析することができます、あなただけのいくつかのポストを使用して、機能を使用することができますパラメーター。

PS:aはちょうどタグ値のリストです。タグはほとんどの場合Stringです。値はanyObject型、リストまたはディクショナリであってもかまいません。そのためには、サーバーから取得したデータを、初期化した変数に適合させる必要があります。

func parseMyJSon(_ username: String, password: String) { 

    var request = URLRequest(url: URL(string: "https://myurlHere")!) // if you have a http please enable the http connection 
    request.httpMethod = "POST" // if you have a get request just change it to get 
    // the post parameters 
    let postString = "user_name=\(username)&password=\(password)" 
    // Get the request :(get the json) 
    request.httpBody = postString.data(using: String.Encoding.utf8) 

    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in 
     guard error == nil && data != nil else { 
      print("error=\(error)") 

      // Do something 

      return 
     } 
     print("the data we have got from the server : \(data!)") 
     // Do Something 
     // think about casting your data to NSDictionnary<String,anyObject> in your case or NSDictionnary<String,[String]> // if your values contains a list of Strings 
    }) 

    task.resume() 
}