简体   繁体   English

如何通过SwiftyJson和Alamofire发布嵌套的json?

[英]How to post nested json by SwiftyJson and Alamofire?

How to post nested json like below as body of method by SwiftyJson and Alamofire?(Swift 3) 如何通过SwiftyJson和Alamofire将嵌套的json作为方法体发布?(Swift 3)

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

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在swift中检查了很多帖子Json post嵌套对象如何使用Alamofire和SwiftyJSON访问嵌套的JSON值? , Alamofire JSON Serialization of Objects and Collections and ... but none of them helped for this situation. Alamofire JSON对象和集合的序列化 ......但是没有一个对这种情况有帮助。

try this 试试这个

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)
    }

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM