【问题标题】:How to post nested json by SwiftyJson and Alamofire?如何通过 SwiftyJson 和 Alamofire 发布嵌套的 json?
【发布时间】:2017-11-13 01:32:25
【问题描述】:

如何通过 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 和... 但他们都没有帮助解决这种情况。

【问题讨论】:

    标签: ios swift swift3 alamofire swifty-json


    【解决方案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)
        }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多