如何通过 SwiftyJson 和 Alamofire 发布嵌套的 json? [英] How to post nested json by SwiftyJson and Alamofire?

查看:26
本文介绍了如何通过 SwiftyJson 和 Alamofire 发布嵌套的 json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 SwiftyJson 和 Alamofire 将如下嵌套的 json 发布为方法体?(Swift 3)

How to post nested json like below as body of method by SwiftyJson and Alamofire?(Swift 3)

{
   "a":{
      "a1": "v1",
      "a2": "v2"
   },
   "b":"bv"
}

我检查了很多帖子 Json 使用 alamofire 快速发布嵌套对象, 我该怎么做使用 Alamofire 和 SwiftyJSON 访问嵌套的 JSON 值?Alamofire JSON 对象和集合序列化 和...但他们都没有帮助解决这种情况.

I check lots of post Json post nested objects in swift using alamofire , How do I access a nested JSON value using Alamofire and SwiftyJSON? , Alamofire JSON Serialization of Objects and Collections and ... but none of them helped for this situation.

推荐答案

试试这个

func test()
    {
        var exampleParameters : [String : Any] = ["b" : "bv"]

        exampleParameters["a"] = ["a1": "v1","a2": "v2"]

        debugPrint(exampleParameters.description)

        let devUrlPush = URL.init(string:"yourURL")

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

        request.httpBody = try! JSONSerialization.data(withJSONObject: exampleParameters)

        Alamofire.request(request).responseJSON { (response) in

            if( response.result.isSuccess)
            {

            }else
            {

            }
        }

        let string = String(data: request.httpBody!, encoding: .utf8)
        let jsonString = JSON(data: request.httpBody!)
        debugPrint(jsonString.rawString(.utf8, options: .prettyPrinted))
        debugPrint(string)
    }

希望能帮到你

这篇关于如何通过 SwiftyJson 和 Alamofire 发布嵌套的 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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