繁体   English   中英

Swift 4 Decodable:解码复杂的JSON

[英]Swift 4 Decodable: Decoding complex JSON

我必须解码字典的数组,其中键是枚举,值是模型对象。

这是我的示例JSON,

[
  {
    "nanomp4": {
      "url": "https://media.tenor.com/videos/a1da4dcf693c2187615721d866decf00/mp4", 
      "dims": [
        150, 
        138
      ], 
      "duration": 2.0, 
      "preview": "https://media.tenor.com/images/17d523e6b7c3c9a4ca64566a1890d94d/tenor.png", 
      "size": 70381
    }, 
    "nanowebm": {
      "url": "https://media.tenor.com/videos/aa983425114e32ab446f669d91611938/webm", 
      "dims": [
        150, 
        138
      ], 
      "preview": "https://media.tenor.com/images/17d523e6b7c3c9a4ca64566a1890d94d/tenor.png", 
      "size": 53888
    }, 
  },
  {
    "nanomp4": {
    "url": "https://media.tenor.com/videos/a1da4dcf693c2187615721d866decf00/mp4",
    "dims": [
          150,
          138
          ],
    "duration": 2.0,
    "preview": "https://media.tenor.com/images/17d523e6b7c3c9a4ca64566a1890d94d/tenor.png",
    "size": 70381
    },
  }
]

这是我的解码代码,

do {
    let data = try Data(contentsOf: fileURL)
    let decoder = JSONDecoder()
    let collection = try decoder.decode([[GIFFormat:Media]].self, from: data)

    print(collection)
} catch {
    print("Error in parsing/decoding JSON: \(error)")
}

这里的GIFFormat是Enum, Media是模型对象,它们解码得很好。

enum GIFFormat: String, Decodable {

    case nanoMP4    = "nanomp4"
    case nanoWebM   = "nanowebm"
}

struct Media: Decodable {
    let url: URL?        
    let dims: [Int]?
    let duration: Double?
    let preview: URL?
    let size: Int64?
}

我的控制台会打印,

typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

谁能告诉我这里到底有什么问题?

即使rawValueGIFFormatStringGIFFormat本身也是一个枚举。 你应该更新

let collection = try decoder.decode([[GIFFormat:Media]].self, from: data)

let collection = try decoder.decode([[GIFFormat.RawValue:Media]].self, from: data)

更新:回应您的评论

现在访问值,我需要像这样使用collection?.first?[GIFFormat.mp4.rawValue]?.url 这又是丑陋!

我建议您进行一些重构。 您可以从完全删除enum开始。 保留您的Media结构。 创建一个新的Collection结构

struct Collection: Decodable {
    let nanomp4: Media!
    let nanowebm: Media!
}

然后,您可以将上面的行更新为

let collection = try decoder.decode([Collection].self, from: data)

你的丑陋的线条变成

collection.first?.nanomp4.url

注意:此解决方案假定您只将nanomp4nanowebm作为枚举值。 如果不是这种情况,那么这可能不是最佳解决方案,您可能不得不采用第一个解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM