How do I host nested json from SwiftyJson and Alamofire?

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

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

      

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

+1


source to share


1 answer


try it

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

+1


source







All Articles