Arrays 解码JSON数组

Arrays 解码JSON数组,arrays,swift,codable,Arrays,Swift,Codable,我正在学习Swift可解码协议,遇到了一个问题。我能够将一个json对象解码成一个swift对象,但我一直在解码数组 进展顺利的是: 想象一下以下json: let json = """ { "all" : { "_id": "59951d5ef2db18002031693c", "text": "America’s cats, including housecats that adventure outdoors and feral cats, kill between 1

我正在学习Swift可解码协议,遇到了一个问题。我能够将一个json对象解码成一个swift对象,但我一直在解码数组

进展顺利的是:

想象一下以下json:

let json = """
{
  "all" : {
    "_id": "59951d5ef2db18002031693c",
    "text": "America’s cats, including housecats that adventure outdoors and feral cats, kill between 1.3 billion and 4.0 billion birds in a year.",
    "type": "cat",
    "user": {
      "_id": "5a9ac18c7478810ea6c06381",
      "name": {
        "first": "Alex",
        "last": "Wohlbruck"
      }
    },
    "upvotes": 4,
    "userUpvoted": null
  }
}
"""

let jsonData = json.data(using: .utf8)
我可以用以下代码将其解码为事实对象:

enum Type: String, Decodable {
    case cat = "cat"
}

struct Fact {
    let id: String
    let text: String
    let type: Type
    let upvotes: Int

    enum CodingKeys: CodingKey {
        case all
    }

    enum FactKeys: CodingKey {
        case _id, text, type, upvotes
    }
}

extension Fact: Decodable {
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let allContainer = try container.nestedContainer(keyedBy: FactKeys.self, forKey: .all)
        id = try allContainer.decode(String.self, forKey: ._id)
        text = try allContainer.decode(String.self, forKey: .text)
        type = try allContainer.decode(Type.self, forKey: .type)
        upvotes = try allContainer.decode(Int.self, forKey: .upvotes)
    }
}

let decoder = JSONDecoder()
let fact = try decoder.decode(Fact.self, from: jsonData!)
但是api给了我一个对象数组:

let json = """
{
  "all": [
    {
      "_id": "59951d5ef2db18002031693c",
      "text": "America’s cats, including housecats that adventure outdoors and feral cats, kill between 1.3 billion and 4.0 billion birds in a year.",
      "type": "cat",
      "user": {
        "_id": "5a9ac18c7478810ea6c06381",
        "name": {
          "first": "Alex",
          "last": "Wohlbruck"
        }
      },
      "upvotes": 4,
      "userUpvoted": null
    },
    {
      "_id": "5b01a447c6914f0014cc9a30",
      "text": "The special sensory organ called the Jacobson's organ allows a cat to have 14 times the sense of smell of a human. It consists of two fluid-filled sacs that connect to the cat's nasal cavity and is located on the roof of their mouth behind their teeth.",
      "type": "cat",
      "user": {
        "_id": "5a9ac18c7478810ea6c06381",
        "name": {
          "first": "Alex",
          "last": "Wohlbruck"
        }
      },
      "upvotes": 4,
      "userUpvoted": null
    }
  ]
}
"""

let jsonData = json.data(using: .utf8)
我想将其存储在一个allFacts数组中,该数组保存我的事实对象

class Facts: ObservableObject {
    @Published var allFacts = [Fact]()
}

let decoder = JSONDecoder()
let allFacts = try decoder.decode([Fact].self, from: jsonData!)
我在我的事实结构上使用相同的扩展。但这给了我一个错误,我完全迷失了一秒钟。你知道我怎么解决这个问题吗? 我也需要为类创建编码键吗

Expected to decode Array<Any> but found a dictionary instead."
应解码数组,但找到了字典。“

我建议不要乱动嵌套容器。这比默认的东西效率低。在您的情况下,您必须使用
nestedUnkeyedContainer
并迭代更昂贵的数组

只需添加一个结构即可

let json = """
{
  "all": [
    {
      "_id": "59951d5ef2db18002031693c",
      "text": "America’s cats, including housecats that adventure outdoors and feral cats, kill between 1.3 billion and 4.0 billion birds in a year.",
      "type": "cat",
      "user": {
        "_id": "5a9ac18c7478810ea6c06381",
        "name": {
          "first": "Alex",
          "last": "Wohlbruck"
        }
      },
      "upvotes": 4,
      "userUpvoted": null
    },
    {
      "_id": "5b01a447c6914f0014cc9a30",
      "text": "The special sensory organ called the Jacobson's organ allows a cat to have 14 times the sense of smell of a human. It consists of two fluid-filled sacs that connect to the cat's nasal cavity and is located on the roof of their mouth behind their teeth.",
      "type": "cat",
      "user": {
        "_id": "5a9ac18c7478810ea6c06381",
        "name": {
          "first": "Alex",
          "last": "Wohlbruck"
        }
      },
      "upvotes": 4,
      "userUpvoted": null
    }
  ]
}
"""

let jsonData = Data(json.utf8)

enum Type: String, Decodable {
    case cat
}

struct Root : Decodable {
    let all : [Fact]
}

struct Fact : Decodable {
    let id: String
    let text: String
    let type: Type
    let upvotes: Int

    enum CodingKeys : String, CodingKey {
        case id = "_id", text, type, upvotes
    }
}

let decoder = JSONDecoder()
let root = try decoder.decode(Root.self, from: jsonData)

print(root.all)

另一个问题,如何将根结构更改为带有@Published var all的ObservebleObject类?这确实是另一个问题,与我不熟悉的Combine框架更相关。