2017-06-20 70 views
0

嗨,這是我的json響應,我如何在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; 

})

代碼API調用行動

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

試試這個:

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

它的工作,但我怎麼可以分配數據到數組中,當我嘗試顯示錯誤「無法將類型'__NSArrayI'()的值轉換爲'NSDictionary'()。」 – 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