Swift Json可解码数据结构,单个数组中的值不同

问题描述 投票:0回答:2

即时通讯对象

"meta_data": [
        {
            "id": 4116,
            "key": "_wcf_frm_created",
            "value": ""
        },
        {
            "id": 4117,
            "key": "_wcf_custom_degin_checkbox",
            "value": ""
        },
        {
            "id": 4118,
            "key": "_wcf_frm_data",
            "value": {
                "1": {
                    "1": "",
                    "2": "",
                    "3": "chk_box"
                }
            }
        },
        {
            "id": 4142,
            "key": "_vendor_select",
            "value": "6484"
        },
        {
            "id": 4143,
            "key": "_vendor_percentage",
            "value": "100"
        },
        {
            "id": 4144,
            "key": "_vendor_pro_cat",
            "value": "Sushi"
        },
        {
            "id": 4156,
            "key": "slide_template",
            "value": "default"
        }
    ],
    "_links": {
        "self": [
            {
                "href": "https://xxxxxx.net/wp-json/wc/v3/products/6489"
            }
        ],
        "collection": [
            {
                "href": "https://xxxxxx.net/wp-json/wc/v3/products"
            }
        ]
    }

我现在有什么

struct woocomerceProduct : Decodable, Encodable
{
    var meta_data : [Meta_data?]
    var _links : [_Links?]


}

    struct Meta_data : Decodable, Encodable
{
    var id : Int?
    var key : String?
    var value : String?
}
   struct One : Decodable, Encodable
{
        var one : String?
        var two : String?
        var three : String?
}

struct _Links : Decodable, Encodable
{
    var SELF : [String?]
    var collectio : [String?]
}

好,所以这里是问题。1. id4118。值从String到obj,如何编码此部分?2.它还使用变量字符串“ 1”,“ 2” ...我不能使用整数作为变量,所以我将其拼写出来了吗?应该可以。3.这里的值是self,我不能使用变量self,因为它会认为这是self属性。所以我只是大写

我看了看,我相信这与我需要做的事情类似,但是由于这是在对象和字符串之间,因此我不确定我需要在此处编写代码。 Swift structures: handling multiple types for a single property

json swift codable swift5 decodable
2个回答
1
投票

尝试使用此链接将json转换为可编码模型link


0
投票

太多不必要的可选内容,太多不必要的下划线字符。

© www.soinside.com 2019 - 2024. All rights reserved.