一尘不染

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

swift

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

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

我使用alamofire检查了许多post Jsonpost嵌套对象,如何使用Alamofire和SwiftyJSON访问嵌套的JSON值?
对象和集合的AlamofireJSON序列化等。但是,在这种情况下,它们都不起作用


阅读 323

收藏
2020-07-07

共1个答案

一尘不染

试试这个

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

我希望这有帮助

2020-07-07