JSON解码SwiftUI

JSON Decode SwiftUI

我在解码 JSON 文件时遇到问题。我正在学习 SwiftUI,并且正在使用 IMDB API。我复制了一个 JSON 文件中的所有证书,我复制到文档中以供离线使用

下面的JSON结构:我没有全部复制,因为它很长但是你可以看到结构 在 swift UI 中,我创建了一个这样的结构:

    struct Certification: Codable {
        let certification: String
        let meaning: String
        let order: Int
    }

我创建了一个 Bundle 扩展来解码使用通用类型的文件,我成功获得了 url / 数据,但 decodedData guard 失败了

guard let decodedData = try? decoder.decode(T.self, from: data) else {
            
            fatalError("Failed to decoded the data from \(decodable)")
        }

我收到致命错误

为此 JSON 声明的正确类型是什么。我如何在 SWIFT 中申报?

我声明了一个全局变量

static let certificates: [String: [Certification]] = Bundle.main.decode("Certifications.json")

我认为 [String: [Certification]] 类型不正确。查看 JSON 文件的正确类型是什么?

感谢您的帮助!

[
    "US": [
      {
        "certification": "G",
        "meaning": "All ages admitted. There is no content that would be objectionable to most parents. This is one of only two ratings dating back to 1968 that still exists today.",
        "order": 1
      },
      {
        "certification": "PG-13",
        "meaning": "Some material may be inappropriate for children under 13. Films given this rating may contain sexual content, brief or partial nudity, some strong language and innuendo, humor, mature themes, political themes, terror and/or intense action violence. However, bloodshed is rarely present. This is the minimum rating at which drug content is present.",
        "order": 3
      },
      {
        "certification": "R",
        "meaning": "Under 17 requires accompanying parent or adult guardian 21 or older. The parent/guardian is required to stay with the child under 17 through the entire movie, even if the parent gives the child/teenager permission to see the film alone. These films may contain strong profanity, graphic sexuality, nudity, strong violence, horror, gore, and strong drug use. A movie rated R for profanity often has more severe or frequent language than the PG-13 rating would permit. An R-rated movie may have more blood, gore, drug use, nudity, or graphic sexuality than a PG-13 movie would admit.",
        "order": 4
      },
      {
        "certification": "NC-17",
        "meaning": "These films contain excessive graphic violence, intense or explicit sex, depraved, abhorrent behavior, explicit drug abuse, strong language, explicit nudity, or any other elements which, at present, most parents would consider too strong and therefore off-limits for viewing by their children and teens. NC-17 does not necessarily mean obscene or pornographic in the oft-accepted or legal meaning of those words.",
        "order": 5
      },
      {
        "certification": "NR",
        "meaning": "No rating information.",
        "order": 0
      },
      {
        "certification": "PG",
        "meaning": "Some material may not be suitable for children under 10. These films may contain some mild language, crude/suggestive humor, scary moments and/or violence. No drug content is present. There are a few exceptions to this rule. A few racial insults may also be heard.",
        "order": 2
      }
    ],
    "CA": [
      {
        "certification": "18A",
        "meaning": "Persons under 18 years of age must be accompanied by an adult. In the Maritimes & Manitoba, children under the age of 14 are prohibited from viewing the film.",
        "order": 4
      },
      {
        "certification": "G",
        "meaning": "All ages.",
        "order": 1
      },
      {
        "certification": "PG",
        "meaning": "Parental guidance advised. There is no age restriction but some material may not be suitable for all children.",
        "order": 2
      },
      {
        "certification": "14A",
        "meaning": "Persons under 14 years of age must be accompanied by an adult.",
        "order": 3
      },
      {
        "certification": "A",
        "meaning": "Admittance restricted to people 18 years of age or older. Sole purpose of the film is the portrayal of sexually explicit activity and/or explicit violence.",
        "order": 5
      }
    ],

您的 JSON 无效,所以我更新了 JSON 并在此处添加了解码部分。我已经在操场上测试过了,效果很好。

let data = Data("""
{
"US": [
  {
    "certification": "G",
    "meaning": "All ages admitted. There is no content that would be objectionable to most parents. This is one of only two ratings dating back to 1968 that still exists today.",
    "order": 1
  },
  {
    "certification": "PG-13",
    "meaning": "Some material may be inappropriate for children under 13. Films given this rating may contain sexual content, brief or partial nudity, some strong language and innuendo, humor, mature themes, political themes, terror and/or intense action violence. However, bloodshed is rarely present. This is the minimum rating at which drug content is present.",
    "order": 3
  },
  {
    "certification": "R",
    "meaning": "Under 17 requires accompanying parent or adult guardian 21 or older. The parent/guardian is required to stay with the child under 17 through the entire movie, even if the parent gives the child/teenager permission to see the film alone. These films may contain strong profanity, graphic sexuality, nudity, strong violence, horror, gore, and strong drug use. A movie rated R for profanity often has more severe or frequent language than the PG-13 rating would permit. An R-rated movie may have more blood, gore, drug use, nudity, or graphic sexuality than a PG-13 movie would admit.",
    "order": 4
  },
  {
    "certification": "NC-17",
    "meaning": "These films contain excessive graphic violence, intense or explicit sex, depraved, abhorrent behavior, explicit drug abuse, strong language, explicit nudity, or any other elements which, at present, most parents would consider too strong and therefore off-limits for viewing by their children and teens. NC-17 does not necessarily mean obscene or pornographic in the oft-accepted or legal meaning of those words.",
    "order": 5
  },
  {
    "certification": "NR",
    "meaning": "No rating information.",
    "order": 0
  },
  {
    "certification": "PG",
    "meaning": "Some material may not be suitable for children under 10. These films may contain some mild language, crude/suggestive humor, scary moments and/or violence. No drug content is present. There are a few exceptions to this rule. A few racial insults may also be heard.",
    "order": 2
  }
]
}
""".utf8)

struct Certification: Codable {
    let certification: String
    let meaning: String
    let order: Int
}

do {
    let decodedData = try JSONDecoder().decode([String: [Certification]].self, from: data)
    print(decodedData)
} catch { print(error) }