gpt4 book ai didi

Swift:无法序列化 Json 响应

转载 作者:行者123 更新时间:2023-11-28 09:26:51 25 4
gpt4 key购买 nike

我正在使用 session 发出 http 请求。这是我的代码:

let task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in
if error != nil {
print(error?.description)
} else {
do {
print("data recived : \(data)")
let jsonContent = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions(rawValue: 0))
print("json content: \(jsonContent)")
}catch{

}
}
}
task.resume()

当我打印数据响应时,我得到了这个:

data recived : Optional(<OS_dispatch_data: data[0x1245c9d60] = { composite, size = 41479, num_records = 3 record[0] = { from = 0, length = 9580, data_object = 0x124584720 }, record[1] = { from = 0, length = 10846, data_object = 0x1245c9a00 }, record[2] = { from = 0, length = 21053, data_object = 0x1245c9f60 }, }>)

你们知道 NSJSONSerialization 不起作用吗?

非常感谢您的帮助。

最佳答案

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(URL, completionHandler: {(data, reponse, error) in

let jsonResults : AnyObject

do {
jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
// success ...
}
catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}

})

task.resume()

关于Swift:无法序列化 Json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846708/

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