7

誰かが私が間違っていることを教えてもらえますか? ここのように、ここのすべての質問を見てきました。Swift Decodable プロトコルを使用してネストされた JSON 構造体をデコードする方法は? そして、まさに必要なものと思われるものを見つけましたSwift 4 Codable デコード json

{
"success": true,
"message": "got the locations!",
"data": {
    "LocationList": [
        {
            "LocID": 1,
            "LocName": "Downtown"
        },
        {
            "LocID": 2,
            "LocName": "Uptown"
        },
        {
            "LocID": 3,
            "LocName": "Midtown"
        }
     ]
  }
}

struct Location: Codable {
    var data: [LocationList]
}

struct LocationList: Codable {
    var LocID: Int!
    var LocName: String!
}

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: "/getlocationlist")

    let task = URLSession.shared.dataTask(with: url!) { data, response, error in
        guard error == nil else {
            print(error!)
            return
        }
        guard let data = data else {
            print("Data is empty")
            return
        }

        do {
            let locList = try JSONDecoder().decode(Location.self, from: data)
            print(locList)
        } catch let error {
            print(error)
        }
    }

    task.resume()
}

私が得ているエラーは次のとおりです。

typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "配列をデコードすることが期待されますが、代わりに辞書が見つかりました.", 基本エラー: nil))

4

3 に答える 3