嗨,這是我的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
}
}
它是可選值。你可以添加代碼嗎? – KKRocks