简体   繁体   English

从 Github api Z818056DBD7E201243206B9C7CD884 解码 JSON 文件时遇到问题

[英]Having trouble decoding JSON file from Github api swift 5

I am trying to decode a JSON format from GitHub API.我正在尝试从 GitHub API 解码 JSON 格式。 The url to the api is correct, it returns all values, but JSONdecoder can't read them. url 到 api 是正确的,它返回所有值,但 JSONdecoder 无法读取它们。 Please help, what am I doing wrong?请帮忙,我做错了什么? I have already done JSONDecoder for github api before, but I only needed the username and repository list, but now I need details about a specific repository我之前已经为 github api 完成了 JSONDecoder,但我只需要用户名和存储库列表,但现在我需要有关特定存储库的详细信息

There is my func do decode JSON:有我的 func 解码 JSON:

func getMoreInfo() -> MoreInfo {
        var moreInfo = MoreInfo(name: "", moreInfoDescription: "", contributorsURL: "", stargazersCount: 0, language: "", forksCount: 0, license: License(key: "", name: ""), topics: [])
        
        if let url = URL(string: "https://api.github.com/repos/allegro/typescript-strict-plugin"){
            URLSession.shared.dataTask(with: url) { data, responde, error in
                if let data = data {
                    do {
                        moreInfo = try JSONDecoder().decode(MoreInfo.self, from: data)
                    } catch let error {
                    print(error)
                    }
                }
            }.resume()
        }
        return moreInfo
    }

There are my structs:有我的结构:

    let name, moreInfoDescription: String
    let contributorsURL: String
    let stargazersCount: Int
    let language: String
    let forksCount: Int
    let license: License
    let topics: [String]

    enum CodingKeys: String, CodingKey {
        case name
        case moreInfoDescription = "description"
        case contributorsURL = "contributors_url"
        case stargazersCount = "stargazers_count"
        case language
        case forksCount = "forks_count"
        case license, topics
    }
}

struct License: Codable {
    let key: String
    let name: String
}

There is the JSON format that gets returned from GitHub API: https://api.github.com/repos/allegro/typescript-strict-plugin There is the JSON format that gets returned from GitHub API: https://api.github.com/repos/allegro/typescript-strict-plugin

Please tell me what I am doing wrong:)请告诉我我做错了什么:)

Your code works well for me, MoreInfo is decoded as required.您的代码对我来说效果很好, MoreInfo已根据需要进行解码。 Using macos 12.2 Beta, Xcode 13.2, targets ios 15 and macCatalyst 12. Tested on real devices.使用 macos 12.2 Beta,Xcode 13.2,针对 ios 15 和 macCatalyst 12。在真实设备上进行了测试。

You of course need to deal with the asynchronous function, as @Larme mentioned.正如@Larme 提到的,您当然需要处理异步 function。 Try this approach:试试这个方法:

func getMoreInfo(completion: @escaping(MoreInfo?) -> ()) {
    if let url = URL(string: "https://api.github.com/repos/allegro/typescript-strict-plugin"){
        URLSession.shared.dataTask(with: url) { data, responde, error in
            if let data = data {
                do {
                    let moreInfo = try JSONDecoder().decode(MoreInfo.self, from: data)
                    completion(moreInfo)
                } catch let error {
                    print(error)
                    completion(nil) // todo deal with errors
                }
            }
        }.resume()
    }
}

and use it like this:

    getMoreInfo() { info in
        print("\n-------> info: \(info)")
    }
        
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM