Swift 4 - 如何构造一个 json 对象并在 switch 中使用 decodable(不工作)

Swift 4 - How to structure a json object and using decodable in switch (not working)

我正在尝试使用 swift 可解码为以下 json 对象创建结构。

{
    "template": [

            {
                "id": 8,
                "question": "Favorite Color?",
                "category": "Color",
                "section": "Favorite Colors",
                "is_active": 1,
            },
            [
                {
                    "id": 14,
                    "question_id": 8,
                    "option_name": "Red",
                    "is_active": 1,
                },
                {
                    "id": 16,
                    "question_id": 8,
                    "option_name": "Orange",
                    "is_active": 1,

                }
            ],
            {
                "id": 9,
                "question": "What cars do you drive?",
                "category": "Cars",
                "section": "Favorite Cars",
                "is_active": 1,

            },
            [
                {
                    "id": 15,
                    "question_id": 9,
                    "option_name": "Toyota",
                    "is_active": 1,
                },
                {
                    "id": 18,
                    "question_id": 9,
                    "option_name": "Honda",
                    "is_active": 1,

                },
                {
                    "id": 19,
                    "question_id": 9,
                    "option_name": "BMW",
                    "is_active": 1,

                }
            ]
        ]

}

我有一些像:

public struct GameTemplate:Decodable {
 question:String?
}
 public struct Game:Decodable {
  let template[GameTemplate]
}

出于某种原因,当我尝试解析它时它不起作用,我收到一条错误消息,指出该结构不是字典。我已经尝试转换结构值,但在这一点上也没有用,只需要在解码后得到一个漂亮干净的 json 对象。

你的JSON格式不一致。

只取color第一类:

{
"template": [

        {
            "id": 8,
            "question": "Favorite Color?",
            "category": "Color",
            "section": "Favorite Colors",
            "is_active": 1,
        },
        [
            {
                "id": 14,
                "question_id": 8,
                "option_name": "Red",
                "is_active": 1,
            },
            {
                "id": 16,
                "question_id": 8,
                "option_name": "Orange",
                "is_active": 1,

            }
        ],
    ]
}

template 是一个 array,在 0 索引上有 dictionary,在 1 索引上有数组。 它可以用不同的方式解码,但需要额外的努力。

如果可能,使 JSON 数据与数组的一个索引中的俱乐部类别一致,如:

{
"template": [

        {
            "id": 8,
            "question": "Favorite Color?",
            "category": "Color",
            "section": "Favorite Colors",
            "is_active": 1,
            "subCategory": [
                       {
                          "id": 14,
                          "question_id": 8,
                          "option_name": "Red",
                          "is_active": 1,
                       },
                       {
                          "id": 16,
                          "question_id": 8,
                          "option_name": "Orange",
                          "is_active": 1,

                       }
              ]
          }
    ]
}

和不同类别的汽车相同的方式。

你很容易解码为:

public struct GameTemplate:Decodable {
    question: String?
    subCategory: [SubCategory]
}

public struct SubCategory:Decodable {
    option_name: String?
}

public struct Game:Decodable {
    let template: [GameTemplate]
}

希望您明白我要解释的内容。