通过POST与Alamofire发送json

时间:2016-05-09 16:09:15

标签: ios json xcode swift alamofire

我有Alamofire的问题,我需要发送一个带有以这种方式格式化的json的POST请求

{
"date":"2016/05/09"
}

这是我的请求代码

static func getCalendar(date: NSDate, completion: (liveBeanList: Array<LiveBean>) -> Void){

    let dateFormatter: NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy/MM/dd"
    let dateString = dateFormatter.stringFromDate(date)


    let jsonObject: [String: AnyObject]  = ["date": dateString]


    Alamofire.request(.POST, "http://127.0.0.1:8080/playground-server/rest/match/calendar", parameters: jsonObject, encoding: .JSON).responseJSON { response in

        if let JSON = response.result.value {
            print("JSON: \(JSON)")

            let jsonResponse = JSON as! NSDictionary
            let response = jsonResponse.objectForKey("response") as! NSDictionary;

            let match_data = response["match_data"] as? [[String: AnyObject]]

            //let liveBean = LiveBean(fromJson: match_data![0] as NSDictionary);

            var liveBeanList: Array<LiveBean> = [];

            for match in match_data! {
                let liveBean = LiveBean(fromJson: match as NSDictionary);
                liveBeanList.append(liveBean)
            }

            completion(liveBeanList: liveBeanList)

        }
    }
}

但在服务器上到达此字符串

data=2016%2F05%2F09

所以它启动一个异常,因为字符串不是json,我怎么能发送一个正确的json?

0 个答案:

没有答案