gpt4 book ai didi

ios - Swift 上的 AFNetworking

转载 作者:搜寻专家 更新时间:2023-11-01 05:44:19 25 4
gpt4 key购买 nike

如何在 AFNetworking、Swift 中获取 JSON 数据?

代码 AFNetworking:

    let manager = AFHTTPRequestOperationManager()
manager.responseSerializer = AFHTTPResponseSerializer()

manager.POST("http://api.address",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in

println("Object: " + responseObject.description)

if let data = responseObject as? Dictionary<String, AnyObject> {
println("JSON: " + "\(data)")
} else {
println("Error JSON")
}

},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})

结果:

对象:<符号>

错误JSON

最佳答案

我在Alamofire中试过了,可以成功转换为Dictionary对象。

    Alamofire.request(.POST, "http://api.address", parameters: nil, encoding:.JSON).responseJSON
{ response in switch response.result {
case .Success(let JSON):

let data = JSON as! Dictionary<String, AnyObject>


case .Failure(let error):
print("Request failed with error: \(error)")
}
}

关于ios - Swift 上的 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com