我收到此错误:“预计解码字典<string,any =”“>但是找到了一个数组。”

时间:2018-03-03 13:44:03

标签: ios json swift swift4 codable

这是我的JSON格式:

[
  {
    "date": "17-06-2015",
    "title": "Night Shift no 58",
    "venue": "Sugarfactory",
    "city": "Amsterdam",
    "country": "NL",
    "fb_url":"https://www.facebook.com/events/00000",
    "ticket_url":"https://shop.eventtickets.com/00000"
  },  
  {
    "date": "24-06-2015",
    "title": "Night Shift no 59",
    "venue": "Sugarfactory",
    "city": "Amsterdam",
    "country": "NL",
    "fb_url":"https://www.facebook.com/events/00000",
    "ticket_url":"https://shop.eventtickets.com/00000"
  }  
]

这是我的实施:

struct Result: Codable {
    let Object: [Event]
}

struct Event: Codable {
    let  date:  String?
    let  title:  String?
    let  venue: String?
    let  city:  String?
    let country: String?
    let fbUrl: String?
    let ticketUrl: String?

    enum CodingKeys: String,CodingKey {
        case  date = "date"
        case title = "title"
        case  venue = "venue"
        case city = "city"
        case country = "country"
        case fbUrl = "fb_url"
        case ticketUrl = "ticket_url"
    }
}

这是我的JSON解析代码:

 if let url = Bundle.main.url(forResource: "events", withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)

            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(Result.self, from: data)
        } catch {
            print("error:\(error)")
        }
    }

0 个答案:

没有答案