Ios GitHub API未解析
这是我从Github获得的JSON的一部分,用于响应请求Ios GitHub API未解析,ios,json,parsing,github-api,codable,Ios,Json,Parsing,Github Api,Codable,这是我从Github获得的JSON的一部分,用于响应请求 { "total_count": 1657, "incomplete_results": false, "items": [ { "id": 68911683, "node_id": "MDEwOlJlcG9zaXRvcnk2ODkxMTY4Mw==", "name": "tetros", "full_name": "daniel-e/tetros", "pr
{
"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)
我正在努力理解描述字段的特殊之处。任何形式的帮助都将不胜感激。谢谢。对于特定的JSON,没有什么不适合描述的。还没有测试过,但这就是您的代码的样子
struct RepoGroup:Codable {
var items:[Repo]
}
struct Repo: Codable {
var fullName:String
var stars:Int
var watchers:Int
var description:String
init(url:String,star:Int,watcher:Int,description:String) {
fullName = url
stars = star
watchers = watcher
description = description
}
enum MyStructKeys: String, CodingKey {
case fullName = "full_name"
case stars = "stargazers_count"
case watchers = "watchers_count"
case description = "description"
}
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)
let description: String = try container.decode(String.self, forKey: .description)
self.init(url: fullName, star: stars, watcher: watchers, description: description)
}
}
Description似乎是GithubAPI中的一个可选字段,当repo没有定义描述时,它将返回为
null
。这意味着您需要将描述字段设置为字符串?
并切换到使用decodeIfPresent
,以说明它是可选的。已尝试使用完全相同的代码。除了描述之外,所有属性似乎都能被正确解析,这让我在过去的几个小时里发疯。是的,这真的很奇怪。我不确定它是否能解决这个问题,但是把“description”变量名改成“desc”怎么样。。这有关系吗?只是随地吐痰,因为我觉得没什么不寻常的。哇,终于奏效了。谢谢。我能请你帮个忙吗。我正在浏览所有的gihub api,但找不到描述可能为空的此信息,请您给我此信息的链接。谢谢。我最终需要在一个描述为空的回购协议上使用他们的搜索API()来找出问题。他们的repoapi文档给出了示例输出,但他们的回答没有考虑到类似的极端情况。好消息是,通常当你在解析中添加一个新字段,它停止工作时,要么是数据格式,要么是你假设需要的可选字段,所以这是我通常尝试的前两件事。真遗憾,由于信息不足浪费了这么多时间。无论如何,我真的很感激你的帮助,否则我不知道还会浪费多少时间。