解析 swift 中的 JSON 数组,对其进行排序并找到重叠的日期

parse JSON array in swift , sort it and find overlapping dates

我该如何解析这个?数组未命名,对象未命名?我还需要对其进行排序并找到事件之间的重叠(识别冲突)如果你们在那里也有任何建议,那将对我有很大帮助。

[{"title": "Evening Picnic", "start": "November 10, 2018 6:00 PM", "end": "November 10, 2018 7:00 PM"}, {"title": "Nap Break", "start": "November 8, 2018 12:56 PM", "end": "November 8, 2018 1:30 PM"}, {"title": "Football Game", "start": "November 3, 2018 6:14 PM", "end": "November 3, 2018 9:44 PM"}, {"title": "Evening Cookout with Friends", "start": "November 6, 2018 5:00 PM", "end": "November 6, 2018 10:00 PM"}, {"title": "Roller Derby", "start": "November 7, 2018 12:00 PM", "end": "November 7, 2018 2:30 PM"}, {"title": "Basketball Game", "start": "November 8, 2018 7:30 PM", "end": "November 8, 2018 10:30 PM"}, {"title": "Local Pub with Friends", "start": "November 1, 2018 7:33 PM", "end": "November 1, 2018 11:00 PM"}, {"title": "Dentist Appointment", "start": "November 10, 2018 1:45 PM", "end": "November 10, 2018 2:30 PM"}, {"title": "Volleyball With Friends", "start": "November 6, 2018 12:00 PM", "end": "November 6, 2018 1:26 PM"}, {"title": "Free Donuts", "start": "November 9, 2018 3:00 PM", "end": "November 9, 2018 4:00 PM"}, {"title": "TV Show Marathon", "start": "November 9, 2018 4:30 PM", "end": "November 9, 2018 9:00 PM"}, {"title": "Boba Tea Grand Opening", "start": "November 6, 2018 1:27 PM", "end": "November 6, 2018 2:00 PM"}, {"title": "Lunch with Friends", "start": "November 8, 2018 11:30 AM", "end": "November 8, 2018 12:59 PM"}, {"title": "SF Coffee Festival","start": "November 6, 2018 6:00 PM","end": "November 6, 2018 9:00 PM"}, {"title": "Beer with Friends", "start": "November 9, 2018 8:00 PM", "end": "November 9, 2018 9:30 PM"}, {"title": "Yoga", "start": "November 1, 2018 6:00 PM", "end": "November 1, 2018 7:33 PM"}, {"title": "Rock Concert", "start": "November 7, 2018 6:30 PM", "end": "November 7, 2018 11:00 PM"}, {"title": "Lunch Meeting", "start": "November 9, 2018 12:30 PM", "end": "November 9, 2018 2:30 PM"}, {"title": "Bicycling with Friends", "start": "November 1, 2018 6:00 AM", "end": "November 1, 2018 9:30 AM"}, {"title": "Birthday Party", "start": "November 10, 2018 12:30 PM", "end": "November 10, 2018 8:30 PM"}, {"title": "Football Tailgate with John", "start": "November 3, 2018 6:14 PM", "end": "November 3, 2018 9:44 PM"}]   

我是 swift 的新手,我想知道如何处理这个问题

您的 JSON 结构中只有两点 "difficult" 可能会导致工作中断。

事实上,它是一个直立 JSON 数组和非标准日期格式。

我建议从一个很好的教程开始,比如 Ultimate Guide to JSON Parsing with Swift,它可能是事实上的来源,因为它几乎涵盖了我迄今为止遇到的所有用例。

首先定义一个结构来保存基本数据并符合 Codable 协议

struct Event: Codable {
    let title: String
    let end: Date
    let start: Date
}

创建 JSONDecoder 并为其提供自定义 dateDecodingStrategy

let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy hh:mm a"

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)

您需要将您的 JSON 回复转换为 Data。我可能已经采用这种格式,或者您可以直接转换 String 文本,这是我为测试所做的

let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy hh:mm a"
formatter.locale = Locale(identifier: "en_US_POSIX")

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(formatter)

最后,解码响应...

let events = try decoder.decode([Event].self, from: data)

从那里您可以开始处理数据。

数组排序和日期比较是一个很容易被问到的问题,可供选择的例子太多了,我不打算在这里重复。做一些研究,尝试一些想法。如果您仍然遇到问题,作为另一个(重点)问题并提供您的尝试

考虑到这是您之前的 . You can use Swift DateInterval 初始化程序的后续问题,用您的 startend 日期创建 DateInverval 并检查它们是否与每个日期相交其他:

extension Event {
    var interval: DateInterval { .init(start: start, end: end) }
    func intersects(with event: Event) -> Bool { interval.intersects(event.interval) }
}

要检查冲突事件,您需要使您的事件符合 Equatable 并过滤与它们相交但不是同一事件的事件:

struct Event: Codable, Equatable {
    let title: String
    let start: Date
    let end: Date
}

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .monthDayYearTime
do {
    let events = try decoder.decode([Event].self, from: Data(json.utf8))
    print(events.sorted())
    let conflictingEvents: [(Event, Event)] = events.compactMap {
        for event in events where event != [=12=] {
            if event.intersects(with: [=12=]) && [=12=].end != event.start && event.end != [=12=].start { return ([=12=], event) }
        }
        return nil
    }
    print(events.count)             // "21\n"
    print(conflictingEvents.count)  // "11\n"
} catch {
    print(error)
}