代码之家  ›  专栏  ›  技术社区  ›  sk123

“[根]”类型的值没有成员“commit”。如何解码JSON根数组

  •  1
  • sk123  · 技术社区  · 7 年前

    Value of type '[Root]' has no member 'commit' . 如何更改此项以打印值。下面是JSON

    [
        {
            "sha": "3f4227ec2894bb354b145deff9dbc1adc6b6d6f2",
            "node_id": "MDY6Q29tbWl0NDQ4Mzg5NDk6M2Y0MjI3ZWMyODk0YmIzNTRiMTQ1ZGVmZjlkYmMxYWRjNmI2ZDZmMg==",
            "commit": {
                "author": {
                    "name": "Slava Pestov",
                    "email": "sviatoslav.pestov@gmail.com",
                    "date": "2018-08-12T08:09:22Z"
                }
            }
        },
        {
            "sha": "3f4227ec2894bb354b145deff9dbc1adc6b6d6f2",
            "node_id": "MDY6Q29tbWl0NDQ4Mzg5NDk6M2Y0MjI3ZWMyODk0YmIzNTRiMTQ1ZGVmZjlkYmMxYWRjNmI2ZDZmMg==",
            "commit": {
                "author": {
                    "name": "Slava Pestov",
                    "email": "sviatoslav.pestov@gmail.com",
                    "date": "2018-08-12T08:09:22Z"
                }
            }
        }
    ]
    

    我在这里解码

    struct Root: Decodable {
        let commit: Author
    }
    
    struct Author: Decodable {
        let author: People
    }
    
    struct People: Decodable {
        let name: String?
        let date: String?
        let email: String?
    }
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            getCommits()
        }
    
    
        func getCommits() {
            let urlString = "https://api.github.com/repos/apple/swift/commits"
            guard let url = URL(string: urlString) else {
                print("Couldn't fetch JSON")
                return
            }
            let session = URLSession.shared
            let dataTask = session.dataTask(with: url) { (data, response, error) in
                guard data != nil && error == nil else {
                    print(data ?? "")
                    return
                }
                do {
                    let decoder = JSONDecoder()
                    let result = try decoder.decode([Root].self, from: data!)
                    print(result.commit)
                    print(result.commit.author.name)
                } catch let decodeError {
                    print("Failed to decode json:", decodeError)
                }
            }
            dataTask.resume()
        }
    }
    

    这是我在控制台的输出。我只想打印姓名、日期和电子邮件。

    [Gihhub.Commits(提交:Gihhub.Author(作者:Gihhub.People(姓名:Optional(“Slava Pestov”),日期:Optional(“2018-08-12T08:09:22Z”),电子邮件:Optional(“sviatoslav.Pestov@gmail.com”)),Gihhub.Commits(提交:Gihhub.Author(作者:Gihhub.People(姓名:Optional(“Slava Pestov”),日期:Optional(“2018-08-12T03:47:22Z”),电子邮件:Optional(“spestov@apple.com”))、Gihhub.Commits(提交:Gihhub.Author(作者:Gihhub.People(姓名:Optional(“Slava Pestov”),日期:Optional(“2018-08-12T03:47:08Z”),电子邮件:Optional(“spestov@apple.com”)))]

    1 回复  |  直到 7 年前
        1
  •  2
  •   Shehata Gamal    7 年前

    结果是需要一个数组

    result.forEach {
        print($0.commit.author.name)
        print($0.commit.author.date)
        print($0.commit.author.email)
    }