2017-06-20 11 views
0

こんにちは、これは私のjsonレスポンスです。どうすれば "data"パラメータ値をArrayに格納できますか?私はAlamofire 4とswift 3を使用しています。これは配列形式で、tableviewで内容をロードするために必要です。Alamofireを使用して辞書内のjsonオブジェクトを解析します。

Optional({ 
"current_page" = 2; 
data =  (
      { 
     "address_location" = "place of course"; 
     "category_id" = 1; 
     "course_id" = 122; 
     "course_title" = title; 
     description = "dessription about the course"; 
     favorited = 0; 
     "from_date" = "2017-02-16"; 
     image = "<path>"; 
     lattitude = "0.876423656"; 
     longitude = "6.86885314"; 
     price = 200; 
     "subcategory_id" = 10; 
     thumb = "<path>"; 
     "to_date" = "2017-02-16"; 
     "user_id" = 32332; 
    }, 
      { 
     "address_location" = "place of course"; 
     "category_id" = 1; 
     "course_id" = 123; 
     "course_title" = title; 
     description = "dessription about the course"; 
     favorited = 0; 
     "from_date" = "2017-02-16"; 
     image = "<path>"; 
     lattitude = "0.876423656"; 
     longitude = "6.86885314"; 
     price = 200; 
     "subcategory_id" = 10; 
     thumb = "<path>"; 
     "to_date" = "2017-02-16"; 
     "user_id" = 1; 
    } 
); 
message = Success; 
"next_page" = 3; 
"previous_page" = 1; 
"status_code" = 200; 

})

Alamofire.request(urlString, method: .get, parameters: ["":""], encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in 

     switch(response.result) { 
     case .success(_): 
      if response.result.value != nil{ 
       print(response.result.value as Any) 

       // 

       self.parseData(JSONData: response.data!) 

       if let jsonDict = response.result.value as? [String:Any], 
        let dataArray = jsonDict["data"] as? [[String:Any]] { 
        self.dictionary_Course = dataArray as? [[String:Any]] 
        let nameArray = dataArray.flatMap { $0["address_location"] as? String } 
        print(nameArray) 
       }      
       print(self.dictionary_Course) 


      } 
      else 
      { 
      } 
      break 

     case .failure(_): 
      print(response.result.error as Any) 
      break 

     } 
    } 
+0

任意の値です。コードを追加できますか? – KKRocks

答えて

0

はこれを試してみてくださいAPI呼び出しアクションの

コード:

Alamofire.request("url", method: .post, parameters: ["":""]).validate().responseJSON { response in 
      switch response.result { 
      case .success: 
       if let result = response.result.value { 
        let responseDict = result as! [String : Any] 
        let data = responseDict["data"] as! [Any] 
        print(data) 
       } 
      case .failure(let error): 
       print(error) 
      } 
     } 
+0

その作業ですが、どのようにデータを配列に割り当てることができますか? "NSDictionary '()に型' __NSArrayI '()の値をキャストできませんでした。 – Lije

+0

どのラインにエラーがありますか? – KKRocks

+0

最新の回答をお試しください。データはあなたの配列です。 – KKRocks

0

スウィフト3.0

てみてくださいコードの下で、あなたが助けてくれるでしょう。

if let result = response.result.value { 
        if let responseDict = result as? NSDictionary { 
         if let data = responseDict.value(forKey: "data") as? 
          [NSDictionary] { 

         } 
         print(data) 
        } 

       } 
+0

オブジェクトを配列または辞書に割り当てる必要があります。どのように私はvar dictionary_Course = [String:String]() – Lije

+0

はい、割り当てることができますが、既にデータがNSDictionaryの配列にあることがわかります。 – Jaydeep

関連する問題