如何使用Swift Decodable属性使用嵌套数组解码嵌套字典?

时间:2018-03-22 08:51:55

标签: ios swift decodable

我需要在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
}

我知道有人可以提供实际格式吗?我实际上对"科目的格式感到困惑,而且#34;它也没有整体编译。

4 个答案:

答案 0 :(得分:2)

有很多问题。

通过部分忽略根对象,你犯了一个常见的错误。

请查看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) }

答案 1 :(得分:0)

看起来你以错误的方式对Class对象进行消毒。它应该看起来像:

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

答案 2 :(得分:0)

这里有一些问题导致你的问题。

  • 您没有顶级项目,我添加了Response struct
  • 位置,类和statusTracker都处于同一级别,而不是在类下。
  • 在您的类结构中,您的项目设置为数组,但它们不是数组
  • 要调试这些类型的问题,请将解码包装在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)
}

答案 3 :(得分:0)

另一种方法是创建一个与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
    }
}