我正在尝试解析可以是
"post" : {
"name" : “Test”,
"profiePic" : "thumb_small_1526912131.png",
"to_uni" : "0",
"id_user" : "2863",
"post_type" : "0",
"likes" : [
],
"tags" : [
],
"to_name" : “Vivek”,
"comments" : [
]
}
或者它可以是以下类型
"post" : {
"name" : “Test2”,
"profiePic" : "thumb_small_1533120835.png",
"to_uni" : "0",
"id_user" : "20393",
"post_type" : "0",
"likes" : {
"message" : “Vivek like this."
},
"tags" : [
],
"to_name" : “Vv”,
"comments" : [
]
}
因此,为了使用 JSONDecoder 解析它,我创建了一个结构如下
并按如下方式解码此json
do {
let responses = try JSONDecoder().decode(Response.self, from: (response?.data!)!)
print(responses)
}catch{
print(error)
}
但是我只是因为喜欢值而收到错误,该值应该解码数组但找到了字典。因为该值在空白时返回字典,而在包含值时返回数组,那么如何在上述结构中管理这种情况?