ios - Swift 4 - 如何构建 json 对象并在 switch 中使用可解码(不起作用)

标签 ios json swift decodable

我正在尝试使用 swift decodeable 为以下 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 格式不一致。

只需取第一类颜色:

{
"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,

            }
        ],
    ]
}

模板是一个数组,在0索引上有字典,在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]
}

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

关于ios - Swift 4 - 如何构建 json 对象并在 switch 中使用可解码(不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52941702/

相关文章:

iOS 图表降低了我的应用程序速度

ios - 使用自定义 iPad 弹出框将箭头与内容 View 对齐

ios - UITableViewCell 圆角对于某些单元格不可见

ios - 在应用程序中更改语言

ios - 如何使 UIAlertController 的消息在 swift2,iOS9 中可复制?

jQuery 解析 JSON

ios - 如何编译 Saurik 的 Veency?

ios - indexPath 处 Row 的高度抛出 EXC Bad Access 错误

javascript - 如何使用具有父子关系的 JSON 数据创建带复选框的树?

javascript - 输入错误值时脚本会重置数据