【问题标题】:GitHub API not parsingGitHub API 未解析
【发布时间】: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


    【解决方案1】:

    描述似乎是 GitHub API 中的一个可选字段,当一个 repo 没有定义描述时,它会以 null 的形式返回。这意味着您需要将描述字段设为 String? 并切换到使用 decodeIfPresent 以说明它是可选的。

    【讨论】:

    • 哇,终于成功了。非常感谢。我可以请你帮个忙吗?我正在浏览所有 gihub api,但找不到描述可能为空的信息,请您给我此信息的链接。谢谢。
    • 我最终需要在一个描述为空的存储库上使用他们的搜索 API (developer.github.com/v3/search) 来找出问题所在。他们的 repo API 文档提供了示例输出,但他们的答案没有考虑到这样的极端情况。好消息,通常当您在解析中添加一个新字段并且它停止工作时,它要么是数据格式,要么是您认为是必需的可选字段,所以这是我通常尝试的前两件事。跨度>
    • 由于信息不足而浪费了这么多时间,真是太可惜了。无论如何,非常感谢您的帮助,否则不知道还要浪费多少时间。
    【解决方案2】:

    关于那个特定的 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)
    }
    
    }
    

    【讨论】:

    • 已经尝试过使用完全相同的代码。除了描述之外,每个属性似乎都能被正确解析,这让我在过去的几个小时里发疯了。
    • 是的,这真的很奇怪。我不确定它是否会解决它,但是将“描述”变量名称更改为“desc”之类的名称怎么样?这有关系吗?只是吐球,因为在我看来没有什么不寻常的。
    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2021-06-20
    • 2017-04-06
    • 2019-03-08
    • 2012-02-02
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多