快速嵌套JSON解析,结构问题

时间:2019-10-26 10:45:02

标签: ios json swift api parsing

我想解析具有嵌套结构的JSON。

我创建了几个带有必填字段的结构,现在尝试使用它们。

这就是JSON(事件API)的样子:

{
  "last_item": null,
   "total_items": "1194",
   "first_item": null,
   "page_number": "1",
   "page_size": "10",
   "page_items": null,
   "search_time": "0.143",
   "page_count": "120",
   "events": {
     "event": [
       {
         "watching_count": null,
         "olson_path": "Europe/London",
         "calendar_count": null,
         "comment_count": null,
         "region_abbr": "London",
         "postal_code": null,
         "going_count": null,
         "all_day": "0",
         "latitude": "51.5009088",
         "groups": null,
         "url": "http://london.eventful.com/events/zucchero-/E0-001-120935308-6?utm_source=apis&utm_medium=apim&utm_campaign=apic",
         "id": "E0-001-120935308-6",
         "privacy": "1",
         "city_name": "London",
         "link_count": null,
         ...

那是我正在尝试做的事情:

struct Event: Codable {

    var id: String
    var title: String
    var latitude: Double
    var longitude: Double
    var url: URL
    var venueAddress: String
    var regionName: String
    var image: [Image]

    enum CodingKeys: String, CodingKey {
        case id
        case title
        case latitude
        case longitude
        case url
        case venueAddress = "venue_address"
        case regionName = "region_name"
        case image

}

struct Events: Codable {
    var events: [Event]
}


let apiUrlString = "http://api.eventful.com/json/events/......."
var event = [Event]()   


func parse(json: Data) {
    DispatchQueue.global().async { [weak self] in
        let decoder = JSONDecoder()
        if let jsonEvents = try? decoder.decode(Events.self, from: json) {
            self?.event = jsonEvents.events
        }
    }
}

@IBAction func searchButtonTapped(_ sender: UIButton) {

    if let url = URL(string: apiUrlString) {
        if let data = try? Data(contentsOf: url) {
            parse(json: data)
            print(event)
        } else {
            showError()
        }
    } else {
        showError()
    }
} 

解析函数可以正常工作,但我的数组始终为空。我想我可以对Event和Events结构进行一些更改。如果您有任何建议,请提供帮助。

1 个答案:

答案 0 :(得分:0)

这是您的json响应的模型,请尝试一下。

// MARK: - Event
struct Event: Codable {
    let lastItem: JSONNull?
    let totalItems: String
    let firstItem: JSONNull?
    let pageNumber, pageSize: String
    let pageItems: JSONNull?
    let searchTime, pageCount: String
    let events: Events

    enum CodingKeys: String, CodingKey {
        case lastItem = "last_item"
        case totalItems = "total_items"
        case firstItem = "first_item"
        case pageNumber = "page_number"
        case pageSize = "page_size"
        case pageItems = "page_items"
        case searchTime = "search_time"
        case pageCount = "page_count"
        case events
    }
}

// MARK: - Events
struct Events: Codable {
    let event: [EventElement]
}

// MARK: - EventElement
struct EventElement: Codable {
    let watchingCount: JSONNull?
    let olsonPath: String
    let calendarCount, commentCount: JSONNull?
    let regionAbbr: String
    let postalCode, goingCount: JSONNull?
    let allDay, latitude: String
    let groups: JSONNull?
    let url: String
    let id, privacy, cityName: String
    let linkCount: JSONNull?

    enum CodingKeys: String, CodingKey {
        case watchingCount = "watching_count"
        case olsonPath = "olson_path"
        case calendarCount = "calendar_count"
        case commentCount = "comment_count"
        case regionAbbr = "region_abbr"
        case postalCode = "postal_code"
        case goingCount = "going_count"
        case allDay = "all_day"
        case latitude, groups, url, id, privacy
        case cityName = "city_name"
        case linkCount = "link_count"
    }
}