Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Alamofire JSON响应,使用Swift中的奇怪字符_Swift_Alamofire - Fatal编程技术网

Alamofire JSON响应,使用Swift中的奇怪字符

Alamofire JSON响应,使用Swift中的奇怪字符,swift,alamofire,Swift,Alamofire,我正在使用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) -&g

我正在使用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\”auth\u token\”=“qcW-mQmyX8ototieJu7WK\”;\n avatar={\n标准={\n url=\”;\n};\n url=\”;\n};\n生日=\”;\n\“创建于\=“2015-10-28T07:02:20.445Z\”;\n电子邮件=\”elrope@abt.com\“;\n\”名字\“=El;\n id=12;\n\”姓\“=Perro;\n role=user;\n\'shooter\u type\”=\“;\n\”更新地址\“=\'2015-10-28T07:42:37.860Z\”;\n}”
既然调用此方法的方法中使用SwiftyJSON的JSON序列化无法将字符串识别为JSON字符串,并且当我执行
let JSON=JSON(reponse)
时失败,您知道如何去掉所有转义字符('\'和'\n')吗

错误域=SwiftyJSONErrorDomain Code=901“字典[“身份验证令牌”] 失败,它不是字典“UserInfo=0x6030002d7c00” {NSLocalizedDescription=Dictionary[“auth_token”]失败,它不是一个 字典})

您的JSON无效

实际上,您的
字符串就是NSDictionary的
.description
输出

操场示范:

不用说,这不是JSON……因此,当您尝试将其提供给SwiftyJSON时,它当然会失败

所以,要么你在某个地方感到困惑,要么你在使用字典的描述,而不是它的实际内容


…或者此错误来自服务器。

我发现您的JSON本身无效。 e、 g.
avatar
standard
不是字符串,引号在哪里

我认为使用
比使用
=
更好


我在SwiftyJSON上进行了测试,在进行上述更改后,一切正常。因此,问题可能出在您的后端。请尝试使用web浏览器验证原始JSON。

我很好奇。编译器说该值是什么类型的?
response:\(值)“
为什么?你为什么这么做?你为什么要它是一根线?如果希望它是字符串,请使用
responseString
,而不是
responseJSON
()。您正在调用
description
,我们可以猜测它是一个JSON,顶层是一个已经解析过的字典。我在调用中将Alamofire从responseJSON()更改为response(),并完成:请求、响应、数据、错误而不是响应。现在数据正常了,我可以用SwiftyJSON正确地序列化它,但仍然不清楚为什么我以前收到来自Alamofire responseJSON()的响应。可能是阿拉莫菲尔的一只虫子?