Swift 4使用' - '字母解码json

时间:2017-12-26 06:06:01

标签: json swift swift4 decodable

swift 4 Decodable协议如何与' - '字母一起使用? 例如:

[{
    "trigger": {
        "url-filter": "webkit.org",
        "resource-type": ["image"]
 },
    "action": {
        "selector": "#logo",
        "type": "block"
    }
}]

在我的Swift课程中:

struct blockerJson : Decodable {
    let action : action
    let trigger : trigger

    struct action : Decodable {
        let selector : String
        let type : String
    }

    struct trigger : Decodable {
        let urlFilter : String
        let resourceType : String
    }
}

我不知道如何更改课程,但json无法改变......

1 个答案:

答案 0 :(得分:1)

此代码解析触发器块

struct Trigger: Decodable {
    var urlFilter: String
    var resourceType: [String]

enum CodingKeys: String, CodingKey {
        case urlFilter = "url-filter"
        case resourceType = "resource-type"
    }
}