为什么 swift 4 codable 在解析时会抛出错误

why does swift 4 codable throw an error when parsing

这是我第一次在应用程序中使用 swift 4 可编码协议。

我收到了这样的 json 回复 -

{
  "result": [
    {
      "id": 1,
      "heading": "Chapter 1",
      "headingTextColor": "#3e9690",
      "title": "Introduction: inovation for crisis",
      "coverImage": "https://www.country067.com/wp-content/uploads/sites/28/2017/07/logoImage_4.jpg",
      "descriptionUrl": "This is an option attribute. If not null this chapter will probably has no sections",
      "sections": [
        {
          "id": 1,
          "title": "Background",
          "url": "http://api.example.com/chapter/1/section/1",
          "projects": "null"
        },
        {
          "id": 2,
          "title": "Projects",
          "url": null,
          "projects": [
            {
              "id": 1,
              "title": "Support to refugees",
              "url": "http://api.example.com/chapter/1/project/1",
              "coverImage": "https://example/wp-content/uploads/sites/28/2017/07/logoImage_4.jpg"
            },
            {
              "id": 2,
              "title": "title text",
              "url": "http://api.example.com/chapter/1/project/2",
              "coverImage": "https://example.com/wp-content/uploads/sites/28/2017/07/logoImage_4.jpg"
            }
          ]
        }
      ]
    }
  ]
}

使用可解码协议,我创建了我的模型来映射响应。

struct ChapterResult: Decodable {
    let result: [Chapter]
}
struct Chapter: Decodable {
    let id: Int
    let heading: String
    let headingTextColor: String
    let title: String
    let coverImage: String
    let descriptionUrl: String
    let sections: [Section]
}

struct Section: Decodable {
    let id: Int
    let title: String
    let url: String?
    let projects: [Project]?
}

struct Project: Decodable {
    let id: Int
    let title: String
    let url: String
    let coverImage: String
}

调用 json 解码器时,我执行以下操作

let decoder = JSONDecoder()
let response = try decoder.decode(ChapterResult.self, from: data)
completion(.success(response.result))

然后导致以下错误

failure(Optional(Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Array but found a string/data instead." UserInfo={NSCodingPath=( "CodingKeys(stringValue: \"result\", intValue: nil)", "_JSONKey(stringValue: \"Index 0\", intValue: 0)", "CodingKeys(stringValue: \"sections\", intValue: nil)", "_JSONKey(stringValue: \"Index 0\", intValue: 0)", "CodingKeys(stringValue: \"projects\", intValue: nil)"

它说 result[0].sections[0].projects 有一个 TypeMissMatch 错误。 所以这个 "projects": "null" 应该是一个数组,但它是 string.

读取错误:

Expected to decode Array but found a string/data instead

在您的 JSON 文件中 projects 有字符串或数组:

"projects": "null"

"projects": [
{
    "id": 1

解决问题 "null" 应该是 null。如果您不能编辑 JSON 文件,您应该编写自定义初始化程序。

看,根据你的结构

struct Section: Decodable {
let id: Int
let title: String
let url: String?
let projects: [Project]? 

}

projectsproject.

类型的数组
{
      "id": 1,
      "title": "Background",
      "url": "http://api.example.com/chapter/1/section/1",
      "projects": "null"
    }

但是在 0th 索引处 sectionsprojects key 的值是 "null" 这是一个字符串,所以这就是您收到错误的原因。

现在仔细阅读你的错误,它包含 "Expected to decode Array but found a string/data instead."