【发布时间】:2020-04-07 15:04:33
【问题描述】:
我在做什么
"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. id 4118. value 从 String 到 obj,我如何编码这部分? 2.它还使用了一个变量字符串“1”,“2”……我不能用整数作为变量,所以我拼出来了?应该可以。 3.这里的值是self,我不能使用变量self,因为它会认为它是一个self属性。所以我只是把它大写了。
我看了这个,我相信这与我需要做的事情相似,但由于这是一个对象和一个字符串之间,我不确定我需要在这里编写什么代码。 Swift structures: handling multiple types for a single property
【问题讨论】:
-
var _links : _Links?必须是一个对象而不是一个数组。
-
==> 对于 SELF,您需要使用编码键。枚举 CodingKeys: String, CodingKey { case SELF = "self" }
标签: json swift codable swift5 decodable