URL使用SwiftyJSON编码Alamofire GET参数

编程入门 行业动态 更新时间:2024-10-15 00:27:34
本文介绍了URL使用SwiftyJSON编码Alamofire GET参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让Alamofire在GET请求中发送以下参数,但它发送乱码:

I am trying to have Alamofire send the following parameter in a GET request but it's sending gibberish:

filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]} //www.example/example?filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]} //Obviously URL encoded

这是我的代码:

let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] let json = JSON(jsonObject) print(json)

输出

{$ and:[ {name:{$ bw:duke}, country:gb} ] }

{ "$and" : [ { "name" : { "$bw" : "duke" }, "country" : "gb" } ] }

这是我的params请求:

This is my params request:

let params = ["filters" : json.rawValue, "limit":"1", "KEY":"my_key"]

这是AlamoFire发送的内容:

This is what AlamoFire is sending:

KEY=my_key& filters[$and][][country]=gb& filters[$and][][name][$bw]=duke& limit=1

正如您所见,filter参数完全混乱。我做错了什么?

As you can see the filter parameter is a complete mess. What am I doing wrong?

推荐答案

默认情况下,Alamofire使用POST正文中的参数列表对参数进行编码。尝试将编码更改为 JSON 。这样,Alamofire会将字典序列化为您想要的JSON字符串:

By default Alamofire encodes the parameters using Parameter List in the POST body. Try changing the encoding to JSON. This way Alamofire will serialize the dictionary as a JSON string as you expect:

let parameters = [ "foo": [1,2,3], "bar": [ "baz": "qux" ] ] Alamofire.request(.POST, "httpbin/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, "httpbin/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 - httpbin/post {"filters":{"$and":[{"name":{"$bw":"duke"},"country":"gb"}]},"limit":"1","KEY":"my_key"}

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

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

If you want to send a URL-Encoded JSON in the GET parameters you have to generate first the JSON string and then pass it as a string in your parameters dictionary:

SWIFT 1

let string = "duke" let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] let json = JSON(jsonObject) // Generate the string representation of the JSON value let jsonString = json.rawString(encoding: NSUTF8StringEncoding, options: nil)! let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"] Alamofire.request(.GET, "httpbin/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 value let jsonString = json.rawString(NSUTF8StringEncoding)! let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"] Alamofire.request(.GET, "httpbin/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 value let jsonString = json.rawString(.utf8)! let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"] Alamofire.request("httpbin/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请求:

This generates a GET request with the following URL:

httpbin/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

该网址已解码为:

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

更多推荐

URL使用SwiftyJSON编码Alamofire GET参数

本文发布于:2023-11-26 02:20:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   URL   SwiftyJSON   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!