JSONDecoder给出了错误的格式

时间:2019-03-28 11:04:33

标签: json swift json-deserialization codable

我有一个.json文件,里面有一些地图,那里有一些带有位置的数组。我想将其解码为一个对象,但是我不断收到错误消息,我的.json格式不正确。它可能是愚蠢的,但是如果有人可以提供帮助,将不胜感激。

JSON文件

{
  "Drenthe" : {
    "gebieden" : {
      "Oostermoer" : {
        "latitude" : "52.3520406",
        "longitude" : "4.6575235",
        "name" : "Oostermoer"
      }
    },
    "overige" : {
      "Bergen op zoom" : {
        "latitude" : "51.4918205",
        "longitude" : "4.2931883",
        "name" : "Bergen op zoom"
      }
    },
    "plaatsen" : {
      "Assen" : {
        "latitude" : "52.9948311",
        "longitude" : "6.5591529",
        "name" : "Assen"
      },
      "Beilen" : {
        "latitude" : "52.857755",
        "longitude" : "6.5162114",
        "name" : "Beilen"
      },
      "Borger" : {
        "latitude" : "52.9243981",
        "longitude" : "6.7912636",
        "name" : "Borger"
      }
    },
    "polders" : {
      "Poldersemeer" : {
        "latitude" : "51.82653820831839",
        "longitude" : "4.257233124070814",
        "name" : "Poldersemeer"
      }
    },
    "provincies" : {
      "Drenthe" : {
        "latitude" : "52.90805",
        "longitude" : "6.6888853",
        "name" : "Drenthe"
      }
    },
    "wateren" : {
      "Drentsche Hoofdvaart" : {
        "latitude" : "52.8420101",
        "longitude" : "6.3372173",
        "name" : "Drentsche Hoofdvaart"
      }
    }
  },
  "Flevoland" : {
    "gebieden" : {
      "Oostermoer" : {
        "latitude" : "52.3520406",
        "longitude" : "4.6575235",
        "name" : "Oostermoer"
      }
    },
    "overige" : {
      "Bergen op zoom" : {
        "latitude" : "51.4918205",
        "longitude" : "4.2931883",
        "name" : "Bergen op zoom"
      }
    },
    "plaatsen" : {
      "bierna" : {
        "latitude" : "46.809093",
        "longitude" : "7.62282",
        "name" : "bierna"
      },
      "bosse" : {
        "latitude" : "11.0559304",
        "longitude" : "-14.4189868",
        "name" : "bosse"
      },
      "dsaf" : {
        "latitude" : "52.5168022",
        "longitude" : "5.7156222",
        "name" : "dsaf"
      },
      "isja" : {
        "latitude" : "25.2934148",
        "longitude" : "32.5525225",
        "name" : "isja"
      },
      "lanes" : {
        "latitude" : "43.9283286",
        "longitude" : "-122.8976901",
        "name" : "lanes"
      }
    },
    "polders" : {
      "Poldersemeer" : {
        "latitude" : "51.82653820831839",
        "longitude" : "4.257233124070814",
        "name" : "Poldersemeer"
      }
    },
    "provincies" : {
      "Drenthe" : {
        "latitude" : "52.90805",
        "longitude" : "6.6888853",
        "name" : "Drenthe"
      }
    },
    "wateren" : {
      "Drentsche Hoofdvaart" : {
        "latitude" : "52.8420101",
        "longitude" : "6.3372173",
        "name" : "Drentsche Hoofdvaart"
      },
      "Hoogeveense Vaart" : {
        "latitude" : "52.724406",
        "longitude" : "6.5762008",
        "name" : "Hoogeveense Vaart"
      }
    }
  }
}

地图和位置结构

struct Location: Codable {
    let name, latitude, longitude : String
}

struct Map: Codable {
    var name: String?
    var plaatsen: [Location]?
    var provincies: [Location]?
    var gebieden: [Location]?
    var polders: [Location]?
    var kanalen: [Location]?
    var wateren: [Location]?
    var overige: [Location]?

    init(
        name: String?,
        plaatsen: [Location]?,
        provincies: [Location]?,
        gebieden: [Location]?,
        polders: [Location]?,
        kanalen: [Location]?,
        wateren: [Location]?,
        overige: [Location]?
    )
    {
        self.name = name
        self.plaatsen = plaatsen
        self.provincies = provincies
        self.gebieden = gebieden
        self.polders = polders
        self.kanalen = kanalen
        self.wateren = wateren
        self.overige = overige
    }
}

获取JSON

// Getting the maps.json file
    if let path = Bundle.main.path(forResource: "maps", ofType: "json") {
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                do {
                    let decoder = JSONDecoder()
                    let mapDecoded = try decoder.decode(Map.self, from: data)
                    print(mapDecoded)
                } catch {
                    print("error trying to convert data to JSON")
                    print(error.localizedDescription)
            }
        } catch {
            // handle error
            print(error.localizedDescription)
        }
    }

修改 我不想使用https://app.quicktype.io之类的东西,因为它们都具有相同的格式,并且我想在json文件中放入更多地图

1 个答案:

答案 0 :(得分:0)

您需要

struct Map: Codable {
    let drenthe: Drenthe

    enum CodingKeys: String, CodingKey {
        case drenthe = "Drenthe"
    }
}

struct Drenthe: Codable {
    let gebieden: Gebieden
    let overige: Overige
    let plaatsen: Plaatsen
    let polders: Polders
    let provincies: Provincies
    let wateren: Wateren
}

struct Gebieden: Codable {
    let oostermoer: Oostermoer

    enum CodingKeys: String, CodingKey {
        case oostermoer = "Oostermoer"
    }
}

struct Oostermoer: Codable {
    let latitude, longitude, name: String
}

struct Overige: Codable {
    let bergenOpZoom: Oostermoer

    enum CodingKeys: String, CodingKey {
        case bergenOpZoom = "Bergen op zoom"
    }
}

struct Plaatsen: Codable {
    let assen, beilen, borger: Oostermoer

    enum CodingKeys: String, CodingKey {
        case assen = "Assen"
        case beilen = "Beilen"
        case borger = "Borger"
    }
}

struct Polders: Codable {
    let poldersemeer: Oostermoer

    enum CodingKeys: String, CodingKey {
        case poldersemeer = "Poldersemeer"
    }
}

struct Provincies: Codable {
    let drenthe: Oostermoer

    enum CodingKeys: String, CodingKey {
        case drenthe = "Drenthe"
    }
}

struct Wateren: Codable {
    let drentscheHoofdvaart: Oostermoer

    enum CodingKeys: String, CodingKey {
        case drentscheHoofdvaart = "Drentsche Hoofdvaart"
    }
}

do {
    let decoder = JSONDecoder()
    let mapDecoded = try decoder.decode(Map.self, from: data)
    print(mapDecoded)
} catch { 
    print(error)   // << Don't forget this 
}

也可以使用

if let url = Bundle.main.url(forResource: "maps", withExtension: "json") { }