我有JSON数据字符串结构,
data: {
"items": [
{
"kind": "customsearch#result",
"title": "XHAOYEAHX 40,60,110,150pcs Shoes Charms Fits ... - Amazon.com",
"snippet": "Buy XHAOYEAHX 40, 60, 110, 150pcs Shoes Charms Fits for Croc Clog Shoes \nWristband Bracelet Party Girls Boys Gifts (Fixed Designs 100pcs) and other ...",
"pagemap": {
"scraped": [
{
"image_link": "https://images-na.ssl-images-amazon.com/images/I/91H%2BUO%2BMDQL._AC_UY395_.jpg"
}
],
"cse_image": [
{
"src": "https://images-na.ssl-images-amazon.com/images/I/91H%2BUO%2BMDQL._AC_UY395_.jpg"
}
]
}
}
]}
字符串
我试着把数据模型结构称为:
struct Items: Codable {
var title:String?
var snippet:String?
var pagemap : PageMap?
enum CodingKeys: String, CodingKey {
case title = "title"
case snippet = "snippet"
case pagemap = "pagemap"
}
init(from decoder: Decoder) throws {
do {
let values = try decoder.container(keyedBy: CodingKeys.self)
title = try values.decode(String.self, forKey: .title)
snippet = try values.decode(String.self, forKey: .snippet)
pagemap = try values.decode(PageMap.self, forKey: .pagemap)
} catch {
print(error.localizedDescription)
}
}
}
struct PageMap: Codable {
var cse_image: [Thumbnail]?
private enum CodingKeys: String, CodingKey {
case cse_image = "cse_image"
}
init(from decoder: Decoder) throws {
do {
let values = try decoder.container(keyedBy: CodingKeys.self)
cse_image = try values.decode([Thumbnail].self, forKey: .cse_image)
} catch {
print(error.localizedDescription)
}
}
}
struct Thumbnail: Codable {
let src: String?
enum CodingKeys: String, CodingKey {
case src = "src"
}
}
型
当我试图调用这个结构并通过dec.keyDecodingStrategy = .convertFromSnakeCase
从src字符串中获取图像数据时,显示错误数据无法读取,因为它丢失了,我错在哪里?
let url = URL(string: "https://www.googleapis.com/customsearch/v1?q=shoes&key=myID&cx=ursearchID")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
print("error: \(error)")
} else {
if let response = response as? HTTPURLResponse {
print("statusCode: \(response.statusCode)")
}
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("data: \(dataString)")
do{
let jsonStr = dataString
let dec = JSONDecoder()
dec.keyDecodingStrategy = .convertFromSnakeCase
let res = try dec.decode(Data.self, from: jsonStr.data(using:.utf8)!)
// self.items = [res]
for shop in res.items{
self.shoptitle.append(shop.title!)
self.shopsnippet.append(shop.snippet!)
self.items.append(shop)
print(shop.snippet)
print(shop.pagemap?.cse_image)
if shop.pagemap?.cse_image != nil {
for info in (shop.pagemap?.cse_image!)! {
print(info.src)
}
}
}
}
catch let error as NSError {
print("\(error)")
}
}
}
}
task.resume()
型
2条答案
按热度按时间sshcrbum1#
首先,您使用的
Codable
模型缺少 JSON 的根级别对象。字符串
重要提示:
1.如果属性名与 JSON 键完全匹配,则不需要
enum CodingKeys
。1.如果你不做任何特定的解析,就不需要
init(from:)
。1.不需要时不要使用
var
。在声明属性时使用let
。1.如果在解析时使用
convertFromSnakeCase
,则必须使用 camel-case(不带下划线)创建属性名。接下来,
URLSession
中的解析代码必须如下所示,型
hjqgdpho2#
这里有几个错误:
1.当您已经有
data
时,您再次将jsonStr
转换为Data
。1.如果你使用的是
convertFromSnakeCase
,所有的蛇形键都将被转换为 Camel 键,例如:cse_image
->cseImage
。Items
,PageMap
不需要自定义CodingKeys
或init(from decoder: Decoder)
。1.你的
JSON
是一个更深的层次,所以,你需要创建一个边界struct
像这样:字符串
dec.decode(Data.self...
尝试解码Data
类型你需要尝试解码Response
。型