Ios Can';无法解码JSON,因为缺少内容类型
My JSONDecoder().decode无法将数据解码为json格式,因为服务器响应的内容类型如下“*\*;charset=utf8” 在这种情况下我该怎么办?有什么想法吗 我的代码:Ios Can';无法解码JSON,因为缺少内容类型,ios,swift,swift4,nsurlsession,codable,Ios,Swift,Swift4,Nsurlsession,Codable,My JSONDecoder().decode无法将数据解码为json格式,因为服务器响应的内容类型如下“*\*;charset=utf8” 在这种情况下我该怎么办?有什么想法吗 我的代码: private static let livePhotoUrlString = "https://m1.kappboom.com/livewallpapers/info?o=0&v=575" static func getLivePhotos(completionHandler: @escapin
private static let livePhotoUrlString = "https://m1.kappboom.com/livewallpapers/info?o=0&v=575"
static func getLivePhotos(completionHandler: @escaping (([LivePhoto]) -> Void)) {
guard let livePhotoUrl = URL(string: livePhotoUrlString) else { return }
let semaphore = DispatchSemaphore(value: 0)
URLSession.shared.dataTask(with: livePhotoUrl) { (data, response, error) in
do {
guard let data = data else { return }
let livePhotos = try JSONDecoder().decode([LivePhoto].self, from: data)
completionHandler(livePhotos)
} catch {
completionHandler([])
}
semaphore.signal()
}.resume()
semaphore.wait()
}
我的实体(现场照片):
响应标题:
正确响应(另一个API):
您需要使用json中的键名,或者使用转换后的名称编写枚举,但最好使用
convertFromSnakeCase
func getLivePhotos(completionHandler: @escaping (([LivePhoto]) -> Void)) {
guard let livePhotoUrl = URL(string: livePhotoUrlString) else { return }
URLSession.shared.dataTask(with: livePhotoUrl) { (data, response, error) in
print(data)
do {
guard let data = data else { return }
let dec = JSONDecoder()
dec.keyDecodingStrategy = .convertFromSnakeCase
let livePhotos = try dec.decode([LivePhoto].self, from: data)
completionHandler(livePhotos)
} catch {
print(error)
completionHandler([])
}
}.resume()
}
}
另外,最好的做法是始终在
catch
块内打印(错误)
,这样您就可以知道错误并进行修复,这里没有信号灯的位置,这只是完成的工作,此外,您还可以显示活动指示符,直到请求作为更好的UX完成。您需要使用json中的键名,或者使用转换后的名称编写枚举,但最好使用convertFromSnakeCase
func getLivePhotos(completionHandler: @escaping (([LivePhoto]) -> Void)) {
guard let livePhotoUrl = URL(string: livePhotoUrlString) else { return }
URLSession.shared.dataTask(with: livePhotoUrl) { (data, response, error) in
print(data)
do {
guard let data = data else { return }
let dec = JSONDecoder()
dec.keyDecodingStrategy = .convertFromSnakeCase
let livePhotos = try dec.decode([LivePhoto].self, from: data)
completionHandler(livePhotos)
} catch {
print(error)
completionHandler([])
}
}.resume()
}
}
另外,最好的做法是始终在
catch
块中打印(错误),这样您就可以知道错误并进行修复,这里没有信号灯的位置,这只是完成的工作,您还可以显示活动指示器,直到请求作为更好的UX完成为止您确定这是问题所在吗,在我看来,您需要为结构定义编码键
enum CodingKeys: String, CodingKey {
case smallUrl = "small_url"
case largeUrl = "large_url"
case movieUrl = "movie_url"
case isLocked = "is_locked"
case promotionalUnlock = "promotional_unlock"
case id
}
您确定这就是问题所在吗?在我看来,您需要为结构定义编码键
enum CodingKeys: String, CodingKey {
case smallUrl = "small_url"
case largeUrl = "large_url"
case movieUrl = "movie_url"
case isLocked = "is_locked"
case promotionalUnlock = "promotional_unlock"
case id
}
该错误与内容类型无关 与其忽略
catch
块print中的错误,解码错误是非常描述性的
} catch {
print(error)
completionHandler([])
}
它说
keyNotFound(编码键(stringValue:“smallUrl”,intValue:nil),Swift.DecodingError.Context(编码路径:[\u JSONKey(stringValue:“Index 0”,intValue:0)],调试说明:“没有与键编码键相关联的值(stringValue:\“smallUrl\”,intValue:nil)(“smallUrl\”),underyingError:nil))
您可以立即看到键是smallUrl
,结构成员是smallUrl
最简单的解决方案是添加convertFromSnakeCase
键解码策略
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let livePhotos = try decoder.decode([LivePhoto].self, from: data)
在类中不需要init
方法。将其声明为具有常量成员的结构
struct LivePhoto: Decodable {
let smallUrl, largeUrl, movieUrl: String
let id: Int
let isLocked: Bool
let promotionalUnlock: Bool
}
请删除这个可怕的信号灯。不管怎样,当您使用完成处理程序时,它是毫无意义的 错误与内容类型无关 与其忽略
catch
块print中的错误,解码错误是非常描述性的
} catch {
print(error)
completionHandler([])
}
它说
keyNotFound(编码键(stringValue:“smallUrl”,intValue:nil),Swift.DecodingError.Context(编码路径:[\u JSONKey(stringValue:“Index 0”,intValue:0)],调试说明:“没有与键编码键相关联的值(stringValue:\“smallUrl\”,intValue:nil)(“smallUrl\”),underyingError:nil))
您可以立即看到键是smallUrl
,结构成员是smallUrl
最简单的解决方案是添加convertFromSnakeCase
键解码策略
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let livePhotos = try decoder.decode([LivePhoto].self, from: data)
在类中不需要init
方法。将其声明为具有常量成员的结构
struct LivePhoto: Decodable {
let smallUrl, largeUrl, movieUrl: String
let id: Int
let isLocked: Bool
let promotionalUnlock: Bool
}
请删除这个可怕的信号灯。不管怎样,当您使用完成处理程序时,它是毫无意义的 谢谢你的回答,这很有帮助。