尝试在iOS中解析动态JSON

时间:2019-02-19 21:35:01

标签: ios json swift codable decodable

我生成了以下JSON示例块。任何以字母结尾的值都是动态的。

{
    "groupName": {
        "groupA": {
            "fields": {
                "fieldA": "valueA",
                "fieldB": "valueB"
            },
            "letters": {
                "letterA: "A"
            }
        },
        "groupB": {
            "fields": {
                "fieldC": "valueC",
                "fieldD": "valueD"
            },
            "letters": {
                "letterB: "B"
            }
        }
    }
}

我的目标是使用Decodable,以便可以将此数据读入已定义的struct s中。

以下是我目前在游乐场文件中包含的工作,我正在使用这些文件来尝试解决此问题:

import Foundation

let jsonString = "{\"groupName\":{\"groupA\":{\"fields\":{\"fieldA\":\"valueA\",\"fieldB\":\"valueB\"},\"letters\":{\"letterA:\"A\"}},\"groupB\":{\"fields\":{\"fieldC\":\"valueC\",\"fieldD\":\"valueD\"},\"letters\":{\"letterB:\"B\"}}}}"

struct CustomCodingKeys: CodingKey {
    var intValue: Int?
    var stringValue: String

    init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
    init?(stringValue: String) { self.stringValue = stringValue }

    static let field = CustomCodingKeys.make(key: "field")

    static func make(key: String) -> CustomCodingKeys {
        return CustomCodingKeys(stringValue: key)!
    }
}

// Values
struct Field {
    let field: String
    let value: String
}

struct Letter: Decodable {
    let title: String
    let letter: String
}

// Value holders
struct FieldData: Decodable {
    var fields: [Field]

    init(from decoder: Decoder) throws {
        self.fields = [Field]()
        let container = try decoder.container(keyedBy: CustomCodingKeys.self)
        for key in container.allKeys {
            print("processing field: \(key.stringValue)")
            let dynamicKey = CustomCodingKeys.make(key: key.stringValue)
            let value = try container.decode(String.self, forKey: dynamicKey)
            let field = Field(field: key.stringValue,
                              value: value)
            fields.append(field)
        }
    }
}

struct LetterData: Decodable {
    var letters: [Letter]

    init(from decoder: Decoder) throws {
        self.letters = [Letter]()
        let container = try decoder.container(keyedBy: CustomCodingKeys.self)
        for key in container.allKeys {
            print("processing letter: \(key.stringValue)")
            let dynamicKey = CustomCodingKeys.make(key: key.stringValue)
            let value = try container.decode(String.self, forKey: dynamicKey)
            let letter = Letter(title: key.stringValue,
                                letter: value)
            letters.append(letter)
        }
    }
}

// Containers
struct Group: Decodable {
    var name: String!
    var groups: [GroupData]

    init(from decoder: Decoder) throws {
        self.groups = [GroupData]()
        let container = try decoder.container(keyedBy: CustomCodingKeys.self)
        for key in container.allKeys {
            print("processing section: \(key.stringValue)")
            let group = try container.decode(GroupData.self,
                                             forKey: key)
            groups.append(group)
        }
    }
}

struct GroupData: Decodable {
    var fieldData: FieldData
    var letterData: LetterData

    enum CodingKeys: String, CodingKey {
        case fieldData = "fields"
        case letterData = "letters"
    }
}

struct GroupList: Decodable {
    struct GroupName: Decodable {
        var name: String!
        var groups: [Group]

        init(from decoder: Decoder) throws {
            self.groups = [Group]()

            let container = try decoder.container(keyedBy: CustomCodingKeys.self)
            for key in container.allKeys {
                let name = key.stringValue
                self.name = name
                print("processing group: \(String(describing: self.name))")
                var group = try container.decode(Group.self,
                                                 forKey: key)
                group.name = name
                groups.append(group)
            }
        }
    }

    let groupName: GroupName
}

let decoder = JSONDecoder()
if let data = jsonString.data(using: .utf8),
    let groupList = try? decoder.decode(GroupList.self,
                                        from: data) {
    print("group list created")
}

在我的GroupData结构中,我可以删除变量,然后实现init(from decoder: Decoder) throws,当使用正确的查询(FieldData和LetterData初始化)进行配置时,它可以标识正确的对。但是,它没有填充适当的值结构。

2 个答案:

答案 0 :(得分:1)

您在解码Group时犯了一个小错误。您倾向于对Group内部的组的所有密钥进行解码,并且还进一步传递对其本身具有“字段”和“字母”的GroupData进行解码。在Group内使用单一值容器,应该没问题。

Group的外观如下,

struct Group: Decodable {
    var name: String!
    var groups: GroupData

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        groups = try container.decode(GroupData.self)
    }
}

请注意,您的json本身不正确,我已经对其进行了格式化,而它应该像这样,

let jsonString = "{\"groupName\":{\"groupA\":{\"fields\":{\"fieldA\":\"valueA\",\"fieldB\":\"valueB\"},\"letters\":{\"letterA\":\"A\"}},\"groupB\":{\"fields\":{\"fieldC\":\"valueC\",\"fieldD\":\"valueD\"},\"letters\":{\"letterB\":\"B\"}}}}"

答案 1 :(得分:-1)

我使用这个https://app.quicktype.io/来帮助我转换它们。 我检查了您的输出,这在行上是错误的:

"letters": {
                "letterA: "A"
            }

正确的: "letterA": "A"

在字母B中出现相同的字母。

正确检查输出:

{
    "groupName": {
        "groupA": {
            "fields": {
                "fieldA": "valueA",
                "fieldB": "valueB"
            },
            "letters": {
                "letterA": "A"
            }
        },
        "groupB": {
            "fields": {
                "fieldC": "valueC",
                "fieldD": "valueD"
            },
            "letters": {
                "letterB": "B"
            }
        }
    }
}