json - JSON 无法使用 Codable 解析
问题描述
我正在尝试解析这个 json 字符串:
let jsonString = """
{
"items": [
{
"snippet": {
"publishedAt": "2020-05-20T16:00:23Z",
"title": "Lesson 2 - One Day Build",
"description": "Description for Lesson 2",
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/PvvwZW-dRwg/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/PvvwZW-dRwg/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"resourceId": {
"videoId": "PvvwZW-dRwg"
}
}
},
{
"snippet": {
"publishedAt": "2020-05-26T16:00:23Z",
"title": "Lesson 3 - One Day Build",
"description": "Description for Lesson 3",
"thumbnails": {
"high": {
"url": "https://i.ytimg.com/vi/m3XbTkMZMPE/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/m3XbTkMZMPE/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"resourceId": {
"videoId": "m3XbTkMZMPE"
}
}
}
]
}
"""
我创建了这些结构(来自 QuickType):
// MARK: - Welcome
struct Welcome: Codable {
let items: Item
}
// MARK: - Item
struct Item: Codable {
let snippet: Snippet
}
// MARK: - Snippet
struct Snippet: Codable {
let publishedAt: Date
let title, snippetDescription: String
let thumbnails: Thumbnails
let resourceID: ResourceID
}
// MARK: - ResourceID
struct ResourceID: Codable {
let videoID: String
}
// MARK: - Thumbnails
struct Thumbnails: Codable {
let high, maxres: High
}
// MARK: - High
struct High: Codable {
let url: String
let width, height: Int
}
我正在调用这个函数:
func ParseJson() {
// Array for the list of video objects
var videos = [Video]()
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
do {
let parsedJson = try decoder.decode(Welcome.self, from: jsonData)
print("here")
} catch {
print(error.localizedDescription)
}
// Output the contents of the array
dump(videos)
} else {
print("Error, unable to parse JSON")
}
}
根据我找到的教程和在线资源,只要这里的结构正确,我应该可以将json字符串解码为结构。输出始终是来自 catch 语句的“操作无法完成”。任何帮助表示赞赏。
谢谢
解决方案
您的代码中有一堆错误:
Welcome.items
是一个Item
s 的数组,而不仅仅是一个。Snippet.publishedAt
是 ISO 8601 格式,所以你需要相应地设置dateDecodingStrategy
解码器的。- 按键拼写错误:
description
, 不是snippetDescription
resourceId
, 不是resouceID
videoId
, 不是videoID
这是更新的结构和解码代码:
// MARK: - Welcome
struct Welcome: Codable {
let items: [Item]
}
// MARK: - Item
struct Item: Codable {
let snippet: Snippet
}
// MARK: - Snippet
struct Snippet: Codable {
let publishedAt: Date
let title, description: String
let thumbnails: Thumbnails
let resourceId: ResourceID
}
// MARK: - ResourceID
struct ResourceID: Codable {
let videoId: String
}
// MARK: - Thumbnails
struct Thumbnails: Codable {
let high, maxres: High
}
// MARK: - High
struct High: Codable {
let url: String
let width, height: Int
}
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {
let parsedJson = try decoder.decode(Welcome.self, from: jsonData)
print("here")
} catch {
print(error)
}
} else {
print("Error, unable to parse JSON")
}
推荐阅读
- sql - 如何更改字符串的格式
- javascript - 为什么我无法将输入数据保存到 AsyncStorage?
- jquery - 每次单击按钮时运行 jQuery 代码
- javascript - ReactTable - 切换行背景颜色 onClick/onExpandRow
- mfc - MFC 应用程序只能在管理员模式下最大化
- sqlite - 如果要使用不同的框架更新应用程序,则提取 Sqlite 数据库
- google-cloud-platform - Google BigTable 中的最大列数
- php - 为 wordpress 中的仪表板重命名 url “localhost/wp-admin”
- python-3.x - python3,多个输入的值异常,正确的顺序?
- hash - 带有 SHa256 的 Hmac 或带有附加在输入字符串中的密钥的 SHa256?哪个更安全?