简体   繁体   English

在 Swift 中解码复杂的嵌套 JSON 4 很多错误

[英]Decoding Complex Nested JSON in Swift 4 Lots of Errors

I am trying to model this nested JSON here into a readable object in Swift, but I am running into lots of unnecessary issues.我正在尝试将 model 这个嵌套的 JSON 转换为可读的 object 在 ZAE832E9B5BDA2699DB45F 中遇到很多不必要的 556 问题。 I want to load the Bible into my app from JSON so I can play with it from a data perspective.我想将圣经从 JSON 加载到我的应用程序中,这样我就可以从数据角度使用它。 Here is my code:这是我的代码:

struct Bible: Codable {
    var book: String
    var chapters: [Chapter]
}
struct Chapter: Codable {
    var chapter: String
    var verses: [Verse]
}
struct Verse: Codable {
    var verse: [String:String]
}

import UIKit
class ViewController: UIViewController {
    var bible: Bible?
    var myText: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        let urlString = "https://raw.githubusercontent.com/aruljohn/Bible-kjv/master/Genesis.json"
        if let url = URL(string: urlString){
            if let data = try? Data(contentsOf: url){
               parse(json: data)
            }
        }
    }//end of viewDidLoad

    func parse(json: Data) {
        let decoder = JSONDecoder()
        if let jsonBible = try? decoder.decode(Bible.self, from: json) {
            bible = jsonBible.self
            bible?.book = jsonBible.book
        }
        myText = bible!.book
        print(myText)
    }//end of parse
}

Here is a small sample of the JSON:这是 JSON 的小样本:

{
"book":"Genesis",
"chapters":[
{
"chapter":"1",
"verses":[
{
"1":"In the beginning God created the heaven and the earth."
},
{
"2":"And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
},
{
"3":"And God said, Let there be light: and there was light."
},
{
"4":"And God saw the light, that it was good: and God divided the light from the darkness."
},
{
"5":"And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
},
{
"6":"And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters."
},
{
"7":"And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so."
},

(Excuse the readability) (原谅可读性)

First of all the model I have will not let me access the data I want to.首先,我拥有的 model 不会让我访问我想要的数据。 And currently now Swift isn't reading one of my fields in the struct even though it's right there.目前 Swift 没有读取我在结构中的一个字段,即使它就在那里。 It was giving me all types of errors so I commented out the chapters field in the Bible struct.它给了我所有类型的错误,所以我注释掉了圣经结构中的章节字段。 I uncomment it and now Swift acts like it's not there, and now I can't access that either.我取消注释它,现在 Swift 就像它不存在一样,现在我也无法访问它。

What did I do wrong?我做错了什么?

Replace代替

var verses: [Verse]

With

var verses: [[String:String]]

try this:尝试这个:

import Foundation

// MARK: - Bible
struct Bible: Codable {
    let book: String
    let chapters: [Chapter]
}

// MARK: - Chapter
struct Chapter: Codable {
    let chapter: String
    let verses: [[String: String]]
}

And to decode the JSON I used this:为了解码 JSON 我使用了这个:

func fetchBible() {
    let urlString = "https://raw.githubusercontent.com/aruljohn/Bible-kjv/master/Genesis.json"
    guard let url = URL(string: urlString) else {
        print("Not url")
        return
    }
    let request = URLRequest(url: url)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print(error!.localizedDescription)
            return
        }
        guard let bible = try? JSONDecoder().decode(Bible.self, from: data) else {
            print("Error decoding JSON")
            return
        }
        DispatchQueue.main.async {
            print(bible)
        }
    }
    task.resume()
}

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

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