为什么某些变量变为nil?

时间:2019-04-20 07:32:57

标签: json swift decoding

我正在尝试解码此json,但某些变量为nil。其中大多数似乎还可以,只是少数无法正常工作。我对快速操作没有太多经验,所以我有点不知所措。

mycode:

struct Attr : Decodable {
        let page: String?
        let perpage: String?
        let totalpages: String?
        let total: String?
    }
    struct Images : Decodable {
        let text: String?
        let size: String?
    }
    struct Artist : Decodable {
        let name: String?
        let mbid: String?
        let url: String?
    }
    struct Streamable : Decodable {
        let text: String?
        let fulltrack: String?
    }
    struct Track : Decodable {
        let name: String?
        let duration: String?
        let playcount: String?
        let listeners: String?
        let mbid: String?
        let url: String?
        let streamable: Streamable?
        let artist: Artist?
        let images: [Images]?
    }
    struct Tracks : Decodable {
        let track:[Track]?
    }
    struct Container : Decodable {
        let tracks: Tracks?
        let attr: Attr?
    }

json:

{
  "tracks": {
    "track": [
      {
        "name": "bad guy",
        "duration": "0",
        "playcount": "870682",
        "listeners": "125811",
        "mbid": "",
        "url": "https://www.last.fm/music/Billie+Eilish/_/bad+guy",
        "streamable": {
          "#text": "0",
          "fulltrack": "0"
        },
        "artist": {
          "name": "Billie Eilish",
          "mbid": "",
          "url": "https://www.last.fm/music/Billie+Eilish"
        },
        "image": [
          {
            "#text": "https://lastfm-img2.akamaized.net/i/u/34s/88d7c302d28832b53bc9592ccb55306b.png",
            "size": "small"
          },
          {
            "#text": "https://lastfm-img2.akamaized.net/i/u/64s/88d7c302d28832b53bc9592ccb55306b.png",
            "size": "medium"
          },
          {
            "#text": "https://lastfm-img2.akamaized.net/i/u/174s/88d7c302d28832b53bc9592ccb55306b.png",
            "size": "large"
          },
          {
            "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/88d7c302d28832b53bc9592ccb55306b.png",
            "size": "extralarge"
          }
        ]
      },
       ...

图像应包含一个图像数组而不是nil,尽管其他大多数变量似乎都还可以

2 个答案:

答案 0 :(得分:0)

将CodingKey枚举添加到名称为#的映射字段

struct Images : Decodable {
    let text: String?
    let size: String?

    enum CodingKeys: String, CodingKey {
        case text = "#text"
        case size
    }
}

struct Streamable : Decodable {
    let text: String?
    let fulltrack: String?

    enum CodingKeys: String, CodingKey {
        case text = "#text"
        case fulltrack
    }
}

您的Track结构中也有错误,请将images更改为image(或在其中使用CodingKey映射)。有关解码json的更多信息,请参见Apple's doc

答案 1 :(得分:0)

这是因为在处理可解码代码时,序列化数据格式中使用的密钥必须与属性名称匹配;在您的情况下,ABC_MESSAGE1 -> 123_MESSAGE1 DEF_MESSAGE2 -> 456_MESSAGE2 GHI_MESSAGE3 -> 789_MESSAGE3 ADF_MESSAGE4 -> ADF_MESSAGE4 ( No change ) 类型包含Imagetext属性,而json包含size#textsize = / = {{ 1}});这也适用于类型名称text而不是#text

但是,citing from Encoding and Decoding Custom Types

  

如果序列化数据格式中使用的密钥与   数据类型中的属性名称,通过提供其他键   将String指定为Images的原始值类型   枚举。

image添加为:

CodingKeys

提示:在解码数据时遇到错误时使用CodingKeys很有用:

struct Images : Decodable {
    let text: String?
    let size: String?

    enum CodingKeys: String, CodingKey {
        case text = "#text"
        case size
    }
}

这时它将显示错误,这可能在大多数时候有助于理解该问题。