簡體   English   中英

如何使用Swift Decodable屬性使用嵌套數組解碼嵌套字典?

[英]How to Decode Nested dictionary with nested arrays using Swift Decodable property?

我需要在swift4中解析的實際JSON是,

{
    "class": {
        "semester1": [
            {
                "name": "Kal"
            },
            {
                "name": "Jack"
            },
            {
                "name": "Igor"
            }
        ],
        "subjects": [
            "English",
            "Maths"
        ]
    },
    "location": {
        "Dept": [
            "EnglishDept",
            ],
        "BlockNo": 1000
    },
    "statusTracker": {
        "googleFormsURL": "beacon.datazoom.io",
        "totalCount": 3000
    }
}

我嘗試過但執行失敗的代碼是

struct Class: Decodable {
    let semester: [internalComponents]
    let location: [location]
    let statusTracker: [statusTracker]
    enum CodingKeys: String, CodingKey {
        case semester = "semester1"
        case location = "location"
        case statusTracker = "statusTracker"
    }
}
struct location: Decodable {
    let Dept: [typesSubIn]
    let BlockNo: Int
}
struct statusTracker: Decodable {
    let googleFormsURL: URL
    let totalCount: Int
}


struct internalComponents: Decodable {
    let semester1: [semsIn]
    let subjects: [subjectsIn]
}
struct semsIn: Decodable {
    let nameIn: String
}
struct subjectsIn: Decodable {
    let subjects: String
}
struct Dept: Decodable {
    let Depts: String
}

我知道有人可以提供實際格式是完全錯誤的嗎? 我實際上對“主題”的格式感到困惑。它也不是整體編譯的。

有很多問題。

通過部分忽略根對象,您經常犯錯誤。

請看一下JSON:在頂層,有3個鍵classlocationstatusTracker 所有3個鍵的值都是字典,沒有數組。

由於class (小寫)是保留字,因此我正在使用components 順便說一句,請遵循結構名稱以大寫字母開頭的命名約定。

struct Root : Decodable {
    let components : Class
    let location: Location
    let statusTracker: StatusTracker

    enum CodingKeys: String, CodingKey { case components = "class", location, statusTracker }
}

還有許多其他問題。 這里是其他結構的合並版本

struct Class: Decodable {
    let semester1: [SemsIn]
    let subjects : [String]
}

struct Location: Decodable {
    let dept : [String]
    let blockNo : Int

    enum CodingKeys: String, CodingKey { case dept = "Dept", blockNo = "BlockNo" }
}

struct SemsIn: Decodable {
    let name: String
}

struct StatusTracker: Decodable {
    let googleFormsURL: String // URL is no benefit
    let totalCount: Int
}

現在解碼Root

do {
    let result = try decoder.decode(Root.self, from: data)
} catch { print(error) }

看起來您以錯誤的方式對Class對象進行了消毒。 它看起來應該像這樣:

struct Class: Decodable {
    let class: [internalComponents]
    let location: [location]
    let statusTracker: [statusTracker]
}

這里有幾件事引起您的問題。

  • 您沒有頂級項目,我添加了Response結構
  • Location,class和statusTracker都處於同一級別,而不是在class之下。
  • 在您的類結構中,您的項目設置為數組,但不是數組
  • 要調試這些類型的問題,請將解碼器包裝在do catch塊中並打印出錯誤。 它會告訴您解析失敗的原因

在我的游樂場嘗試以下代碼:

let jsonData = """
{
    "class": {
        "semester1": [{
            "name": "Kal"
        }, {
            "name": "Jack"
        }, {
            "name": "Igor"
        }],
        "subjects": [
            "English",
            "Maths"
        ]
    },
    "location": {
        "Dept": [
            "EnglishDept"
        ],
        "BlockNo": 1000
    },
    "statusTracker": {
        "googleFormsURL": "beacon.datazoom.io",
        "totalCount": 3000
    }
}
""".data(using: .utf8)!

struct Response: Decodable {
    let cls: Class
    let location: Location
    let statusTracker: statusTracker

    enum CodingKeys: String, CodingKey {
        case cls = "class"
        case location
        case statusTracker
    }
}

struct Class: Decodable {
    let semester: [SemesterStudents]
    let subjects: [String]

    enum CodingKeys: String, CodingKey {
        case semester = "semester1"
        case subjects
    }
}

struct Location: Decodable {
    let dept: [String]
    let blockNo: Int

    enum CodingKeys: String, CodingKey {
        case dept = "Dept"
        case blockNo = "BlockNo"
    }
}

struct statusTracker: Decodable {
    let googleFormsURL: URL
    let totalCount: Int
}

struct SemesterStudents: Decodable {
    let name: String
}

struct Dept: Decodable {
    let Depts: String
}

do {
    let result = try JSONDecoder().decode(Response.self, from: jsonData)
    print(result)
} catch let error {
    print(error)
}

另一種方法是創建一個與JSON緊密匹配的中間模型,讓Swift生成對它進行解碼的方法,然后從最終數據模型中挑選出所需的片段:

// snake_case to match the JSON
fileprivate struct RawServerResponse: Decodable {
    struct User: Decodable {
        var user_name: String
        var real_info: UserRealInfo
    }

    struct UserRealInfo: Decodable {
        var full_name: String
    }

    struct Review: Decodable {
        var count: Int
    }

    var id: Int
    var user: User
    var reviews_count: [Review]
}

struct ServerResponse: Decodable {
    var id: String
    var username: String
    var fullName: String
    var reviewCount: Int

    init(from decoder: Decoder) throws {
        let rawResponse = try RawServerResponse(from: decoder)

        // Now you can pick items that are important to your data model,
        // conveniently decoded into a Swift structure
        id = String(rawResponse.id)
        username = rawResponse.user.user_name
        fullName = rawResponse.user.real_info.full_name
        reviewCount = rawResponse.reviews_count.first!.count
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM