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

时间:2017-06-11 14:09:21

标签: ios swift swift3 alamofire swifty-json

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

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

我查看了大量帖子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和... 但是他们都没有帮助这种情况。

1 个答案:

答案 0 :(得分:2)

试试这个

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

我希望这会有所帮助