【发布时间】:2019-12-30 04:55:33
【问题描述】:
这是我从 Github 获取的响应请求的 JSON 的一部分
{
"total_count": 1657,
"incomplete_results": false,
"items": [
{
"id": 68911683,
"node_id": "MDEwOlJlcG9zaXRvcnk2ODkxMTY4Mw==",
"name": "tetros",
"full_name": "daniel-e/tetros",
"private": false,
"html_url": "https://github.com/daniel-e/tetros",
"description": "Tetris that fits into the boot sector.",
"size": 171,
"stargazers_count": 677,
"watchers_count": 677,
"language": "Assembly",
}
]
}
这是我的模特
struct RepoGroup:Codable {
var items:[Repo]
}
struct Repo: Codable {
var fullName:String
var stars:Int
var watchers:Int
init(url:String,star:Int,watcher:Int) {
fullName = url
stars = star
watchers = watcher
}
enum MyStructKeys: String, CodingKey {
case fullName = "full_name"
case stars = "stargazers_count"
case watchers = "watchers_count"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MyStructKeys.self)
let fullName: String = try container.decode(String.self, forKey: .fullName)
let stars: Int = try container.decode(Int.self, forKey: .stars)
let watchers: Int = try container.decode(Int.self, forKey: .watchers)
self.init(url: fullName, star: stars, watcher: watchers)
}
}
到目前为止一切顺利。但是一旦我在我的模型中添加description:String 字段,JSON 解码器就莫名其妙地无法解析。
这是我的解析器
let model = try JSONDecoder().decode(RepoGroup.self, from: dataResponse)
我很难理解描述字段有什么特别之处。任何形式的帮助将不胜感激。谢谢。
【问题讨论】:
标签: ios json parsing github-api codable