为什么某些变量会变为零?

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

我正在尝试解码这个json,但某些变量是零。大多数这些似乎都没关系,只有少数不能正常工作。我对swift没有多少经验,所以我有点不知道接下来会尝试什么。

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"
          }
        ]
      },
       ...

图像应该包含一个Images数组而不是nil,但其他大多数变量似乎都没问题

json swift decoding
2个回答
0
投票

这是因为在处理可解码时,序列化数据格式中使用的密钥必须与属性名称匹配;在你的情况下,Image类型包含textsize属性,但json包含#textsizetext = / = #text);这也适用于类型名称Images而不是image

但是,citing from Encoding and Decoding Custom Types

如果序列化数据格式中使用的键与数据类型中的属性名称不匹配,请通过将String指定为CodingKeys枚举的原始值类型来提供备用键。

添加CodingKeys为:

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

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

提示:在解码数据时遇到错误时使用try/catch非常有用:

do {
    let result = try JSONDecoder().decode(Images.self, from: data)
} catch {
    print(error.localizedDescription)
}

此时它将打印错误,这可能有助于在大多数时间理解该问题。


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
    }
}

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

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