JSON 内部对象 JSON 对象 Swift

JSON object inside JSON object Swift

这是我的 JSON 文件。

{
    "id": 3498,
    "name": “Fetcher”,
    "description": “BlaBlaBla”,
    "released": "1993-11-11",
        "platforms": [
        {
            "platform": {
                "name": “Anaconda”,
            }
         },
         {
            "platform": {
                "name": “Cobra”,
            },
        }],
}

我有以下结构:

struct Markers: Codable {
   var id : Int?
   var name : String?
   var description : String?
   var released : String?
   var platforms : [Platforms]? ///If I comment this it works fine, otherwise it won't 
}

struct Platforms: Codable {
   var platform : [Platform]?
}

struct Platform: Codable {
   var name : String?
}

如果我在结构中使用平台,我将无法捕获数据。如果我在标记结构中评论平台,我将在数据响应中获得所有内容,但对于平台,它会打印“仍然不起作用”

   do{
      let fetData = try JSONDecoder().decode(Markers.self, from:data)
      debugPrint(fetData)
     }
   catch{
       print("Still doesn't work")
    }

替换

var platform : [Platform]?

var platform : Platform?

platform 是对象不是数组