Alamofire 3.0 处理 JSON

Posted

技术标签:

【中文标题】Alamofire 3.0 处理 JSON【英文标题】:Alamofire 3.0 handle JSON 【发布时间】:2015-11-13 15:05:24 【问题描述】:

对不起我的英语不好:)

所以我们开始吧。我开始在 Swift 2 中创建 iPhone 应用程序。 我想使用 API。对于请求,我使用了 Alamofire 3.0 库。这工作正常,但我无法处理 JSON。我尝试使用 SwiftyJSON,但我不知道它是如何工作的。有我的代码:

let headers = [
    "Content-Type": "application/json"
]

    Alamofire.request(.GET, "API URL", headers: headers)
        .responseJSON  response in

            if response.result.isSuccess 

            

    

我希望有人可以帮助我。 ;) 谢谢

【问题讨论】:

看看这个***.com/a/33079355/3734028 【参考方案1】:

我最近使用 Alamofire (3.0) 和 SwiftyJSON (2.3)

let parameters = ["param1" : param, "param2" : "stringParam"] // my params, not required
Alamofire.request(.POST, url, parameters: parameters)
    .responseJSON response in
        guard response.result.error == nil else 
            print("Error. \(response.result.error?.localizedDescription)")
                return
             // guard close
            if response.result.isSuccess 
                let post = JSON(response.result.value!) // JSON is stored in post variable
            // Another check of result
            //if let value: AnyObject = response.result.value 
            //    let post = JSON(value)
             // if close
      // responseJSON close

然后按照Github中的说明访问JSON

希望对你有帮助

杰克

【讨论】:

以上是关于Alamofire 3.0 处理 JSON的主要内容,如果未能解决你的问题,请参考以下文章