【发布时间】:2015-10-28 12:19:41
【问题描述】:
我正在使用 Alamofire 3.0 和 swift 2,并从以下请求中得到一个奇怪的响应:
func requestURLwithHeadersAndParams(URLstr:String, connectionMethod: Alamofire.Method, header: [String : String], body:[String : AnyObject], completion: (reponse: String, statusCode: Int, error: String) -> Void) {
///Singleton Alamofire connection manager
let alamofireManager = Alamofire.Manager.sharedInstance
alamofireManager.request(connectionMethod, URLstr, parameters: body, encoding: ParameterEncoding.JSON, headers: header).responseJSON (){
response in
print("\nSuccess: \(response.result.isSuccess)")
switch response.result {
case .Success(let value):
print("Response Status code: \((response.response?.statusCode)!)\n")
print("\(value)")
completion(reponse: "\(value)" , statusCode: (response.response?.statusCode)! , error: "")
case .Failure(let error):
print("Error Code:\(error.code) - Description:\(error.localizedDescription)")
completion(reponse: "Error" , statusCode: (response.response?.statusCode)!, error: error.localizedDescription)
} } }
value 属性包含:
"{\n \"auth_token\" = \"qcW-mQmyX8ototieJu7WK\";\n avatar = {\n standard = {\n url = \"<null>\";\n };\n url = \"<null>\";\n };\n birthdate = \"<null>\";\n \"created_at\" = \"2015-10-28T07:02:20.445Z\";\n email = \"elrope@abt.com\";\n \"first_name\" = El;\n id = 12;\n \"last_name\" = Perro;\n role = user;\n \"shooter_type\" = \"\";\n \"updated_at\" = \"2015-10-28T07:42:37.860Z\";\n}"
知道如何摆脱所有转义字符('\' 和 '\n'),因为在调用此方法的方法中使用 SwiftyJSON 的 JSON 序列化无法将字符串识别为 json 字符串并且在我做let json = JSON(reponse) 有错误:
错误域=SwiftyJSONErrorDomain 代码=901 "字典["auth_token"] 失败,它不是字典” UserInfo=0x6030002d7c00 {NSLocalizedDescription=Dictionary["auth_token"] 失败,它不是 字典})
【问题讨论】:
-
我很好奇。编译器说这个值是什么类型?
-
reponse: "\(value)"为什么?你为什么这样做?为什么你希望它是一个字符串?如果您希望它是一个字符串,请使用responseString,而不是responseJSON(stackoverflow.com/questions/44095161/…)。您正在调用description我们可以猜测的是一个 JSON,其顶层是一个已经解析的字典。