这是我的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)")
}
}