快速发布json请求

编程入门 行业动态 更新时间:2024-10-25 11:35:45
本文介绍了快速发布json请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道如何发布简单的 json:

I know how to post simple json:

// Compose a query string let postString = "firstName=James&lastName=Bond"; request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

现在服务器端需要这样的json格式:

Now the server side require the json format like:

{ "name": "testuser123", "pass": "testuser123", "field_shouji": { "und": [{ "value": "15652344931" }] } }

并且内容类型应该是:application/json.我已经用谷歌搜索了一整天,仍然可以找到正确的方法.

and the content type should be: application/json. I have googled the whole day, still could find the right way.

推荐答案

您需要发送有效格式的 JSON.firstName=James&lastName=Bond"; 不是 JSON格式.

You need to send a valid format of JSON."firstName=James&lastName=Bond"; is not JSON format.

自从 Swift 3 和 4 Apple 开始移除 NS* 的东西(即使其中很多只是 NS* 的别名),我们将使用更多的 Swift 方式

Since Swift 3 and 4 Apple is starting to remove NS* stuff (even that many of them are just aliases to NS*) we'll use more Swift way

//Method just to execute request, assuming the response type is string (and not file) func HTTPsendRequest(request: URLRequest, callback: @escaping (Error?, String?) -> Void) { let task = URLSession.shared.dataTask(with: request) { (data, res, err) in if (err != nil) { callback(err,nil) } else { callback(nil, String(data: data!, encoding: String.Encoding.utf8)) } } task.resume() } // post JSON func HTTPPostJSON(url: String, data: Data, callback: @escaping (Error?, String?) -> Void) { var request = URLRequest(url: URL(string: url)!) request.httpMethod = "POST" request.addValue("application/json",forHTTPHeaderField: "Content-Type") request.addValue("application/json",forHTTPHeaderField: "Accept") request.httpBody = data HTTPsendRequest(request: request, callback: callback) }

使用示例

var dict = Dictionary<String, Any>() dict["username"] = "hello" dict["password"] = "swift" let data = try JSONSerialization.data(withJSONObject: dict, options: []) HTTPPostJSON(url: "example/login", data: data) { (err, result) in if(err != nil){ print(err!.localizedDescription) return } print(result ?? "") }

斯威夫特 <4

func HTTPsendRequest(request: NSMutableURLRequest, callback: (String, String?) -> Void) { let task = NSURLSession.sharedSession() .dataTaskWithRequest(request) { (data, response, error) -> Void in if (error != nil) { callback("", error.localizedDescription) } else { callback(NSString(data: data, encoding: NSUTF8StringEncoding)! as String, nil) } } task.resume() } func HTTPPostJSON(url: String, data: NSData, callback: (String, String?) -> Void) { var request = NSMutableURLRequest(URL: NSURL(string: url)!) request.HTTPMethod = "POST" request.addValue("application/json",forHTTPHeaderField: "Content-Type") request.addValue("application/json",forHTTPHeaderField: "Accept") request.HTTPBody = data HTTPsendRequest(request, callback: callback) }

使用示例

我们将设置 NSMutableDictionary 并将其转换为 JSON

we'll set NSMutableDictionary and convert it to JSON

var json = NSMutableDictionary() json.setValue("testuser123", forKey: "name"); //set all your values.. let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions(0), error: nil); HTTPPostJSON("http;..", data:data!) { (response, error) -> Void in println(response); }

更多推荐

快速发布json请求

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

发布评论

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

>www.elefans.com

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