Swift Codable-无法解析包含数组或字典类型的JSON

时间:2018-09-10 14:52:21

标签: json swift codable decodable jsondecoder

我正在尝试解析JSON

"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对此进行解析,我创建了一个结构如下

Codable Structure dpaste link

并按如下方式解码此json

do {
                let responses = try JSONDecoder().decode(Response.self, from: (response?.data!)!)
                print(responses)
            }catch{
                print(error)
            }

但是我只是因为喜欢值而得到错误,该值是预期对数组进行解码,但却找到了一个字典。,因为该值空白时包含字典时返回字典,所以返回数组,因此如何管理上面的结构中有这种情况?

0 个答案:

没有答案
相关问题