栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

URL使用SwiftyJSON编码Alamofire GET参数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

URL使用SwiftyJSON编码Alamofire GET参数

默认情况下,Alamofire使用POST正文中的“参数列表”对参数进行编码。尝试将编码更改为

JSON
。这样,Alamofire可以按您期望的那样将字典序列化为JSON字符串:

let parameters = [    "foo": [1,2,3],    "bar": [        "baz": "qux"    ]]Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters, encoding: .JSON)// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}

或使用您的代码:

let string = "duke"let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]]let json = JSON(jsonObject)let params = ["filters" : json.rawValue, "limit":"1", "KEY":"my_key"]Alamofire.request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)    .responseString(encoding: NSUTF8StringEncoding) { request, response, content, error in        NSLog("Request: %@ - %@n%@", request.HTTPMethod!, request.URL!, request.HTTPBody.map { body in NSString(data: body, encoding: NSUTF8StringEncoding) ?? "" } ?? "")        if let response = response { NSLog("Response: %@n%@", response, content ?? "")        }}

获取输出:

Request: POST - http://httpbin.org/post{"filters":{"$and":[{"name":{"$bw":"duke"},"country":"gb"}]},"limit":"1","KEY":"my_key"}

编辑:GET参数中的URL编码的JSON

如果要在GET参数中发送URL编码的JSON,则必须首先生成JSON字符串,然后将其作为字符串传递到参数字典中:

SWIFT 1

let string = "duke"let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]]let json = JSON(jsonObject)// Generate the string representation of the JSON valuelet jsonString = json.rawString(encoding: NSUTF8StringEncoding, options: nil)!let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"]Alamofire.request(.GET, "http://httpbin.org/post", parameters: params)    .responseString(encoding: NSUTF8StringEncoding) { request, response, content, error in        NSLog("Request: %@ - %@n%@", request.HTTPMethod!, request.URL!, request.HTTPBody.map { body in NSString(data: body, encoding: NSUTF8StringEncoding) ?? "" } ?? "")        if let response = response { NSLog("Response: %@n%@", response, content ?? "")        }}

SWIFT 2

let string = "duke"let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]]let json = JSON(jsonObject)// Generate the string representation of the JSON valuelet jsonString = json.rawString(NSUTF8StringEncoding)!let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"]Alamofire.request(.GET, "http://httpbin.org/post", parameters: params)    .responseString(encoding: NSUTF8StringEncoding) { request, response, result in        NSLog("Request: %@ - %@n%@", request!.HTTPMethod!, request!.URL!, request!.HTTPBody.map { body in NSString(data: body, encoding: NSUTF8StringEncoding) ?? "" } ?? "")        switch result {        case .Success(let value): NSLog("Response with content: %@", value)        case .Failure(let data, _): NSLog("Response with error: %@", data ?? NSData())        }}

SWIFT 3和Alamofire 4.0

let string = "duke"let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]]let json = JSON(jsonObject)// Generate the string representation of the JSON valuelet jsonString = json.rawString(.utf8)!let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"]Alamofire.request("http://httpbin.org/post", method: .get, parameters: params)    .responseString { response in        #if DEBUG let request = response.request NSLog("Request: (request!.httpMethod!) - (request!.url!.absoluteString)n(request!.httpBody.map { body in String(data: body, encoding: .utf8) ?? "" } ?? "")") switch response.result { case .success(let value):     print("Response with content (value)") case .failure(let error):     print("Response with error: (error as NSError): (response.data ?? Data())") }        #endif}

这将生成具有以下URL的GET请求:

http://httpbin.org/post?KEY=my_key&filters=%7B%22%24and%22%3A%5B%7B%22name%22%3A%7B%22%24bw%22%3A%22duke%22%7D%2C%22country%22%3A%22gb%22%7D%5D%7D&limit=1

URL解码为:

http://httpbin.org/post?KEY=my_key&filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]}&limit=1


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号