如何使用alamofire将多个JSON对象作为流发送 [英] How to send multiple JSON objects as a stream using alamofire

查看:51
本文介绍了如何使用alamofire将多个JSON对象作为流发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个请求中发送多个不同的JSON对象,我觉得在单个请求中像文件一样流式传输多个JSON对象会更好,所以请告诉我是否可行,如果可以的话请给我一个如何使用Alamofire进行操作的想法,下面是我要发布的原始正文(应用程序/json)数据的格式

I want to send multiple different JSON object in a single request, i felt streaming the multiple JSON objects like a file in a single request would be better, so kindly let me know if it is possible, if so kindly give me an idea of how to do it using Alamofire, Below is the formate of the raw body (application/json) data that i want to post

{"a":23214,"b":54674,"c":"aa12234","d":4634}
{"a":32214,"b":324674,"c":"as344234","d":23434}
{"a":567214,"b":89674,"c":"sdf54234","d"34634}

我尝试了以下代码,但是由于body param的格式不正确,因此无法正常工作,这就是我想在单个请求中尝试将多个JSON对象作为流发送的原因,敬请注意

I tried the below code, but it didn't work as the body param is not in the correct format, that's the reason i want to try sending the multiple JSON objects as a stream in a single request, Kindly advice

let SendParams = [
      ["a":1234, "b":2345, "c":3456], ["a":2345, "b":3456, "c":4567], ["a":3456, "b":4567, "c":5678]
    ]
_ = Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding, headers: header)

推荐答案

JSON 格式不适用于在您的请求中,JSON始终是键值对,其中key始终是String,值是任何Object.在您的示例中,需要为Array类型的对象设置顶级密钥,如下所示:

JSON format is not correct for your request, JSON is always key-value pair, where key is always String, and value is any Object. In your example need to set key at top level for object of type Array as below:

let SendParams = [
                    "key" :[["a":1234, "b":2345, "c":3456], ["a":2345, "b":3456, "c":4567], ["a":3456, "b":4567, "c":5678]]
                ]
_ = Alamofire.request(url, method: .post, parameters: SendParams, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in}

OR

序列化数组并将其设置为URLRequest对象的httpBody:

Serialise the array and set as httpBody of URLRequest object:

    let url = YOUR_POST_API_URL
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    let values = [
        ["a":1234, "b":2345, "c":3456], ["a":2345, "b":3456, "c":4567], ["a":3456, "b":4567, "c":5678]
    ]
    request.httpBody = try! JSONSerialization.data(withJSONObject: values)

    Alamofire.request(request)
        .responseJSON { response in
            // do whatever you want here
            switch response.result {
            case .failure(let error):
                print(error)

                if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
                    print(responseString)
                }
            case .success(let responseObject):
                print(responseObject)
            }
    }

这篇关于如何使用alamofire将多个JSON对象作为流发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆