无法解析 swift 中的 json 数据

Unable to parse a json data in swift

我有一个 API 回复如下:

{
    "status": 200,
    "schedule_data": {
        "2020_08_16_1597485600": {
            "schedule_date": "2020-08-16",
            "nanny_id": 2,
            "shift_start": "06:00",
            "shift_end": "13:00",
            "schedule_id": 112
        },
        "2020_08_16": {
            "schedule_date": "2020-08-16",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_17_1597485600": {
            "schedule_date": "2020-08-17",
            "nanny_id": 2,
            "shift_start": "06:00",
            "shift_end": "22:00",
            "schedule_id": 113
        },
        "2020_08_17": {
            "schedule_date": "2020-08-17",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_18": {
            "schedule_date": "2020-08-18",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_19": {
            "schedule_date": "2020-08-19",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_20": {
            "schedule_date": "2020-08-20",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_21": {
            "schedule_date": "2020-08-21",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_22": {
            "schedule_date": "2020-08-22",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_23": {
            "schedule_date": "2020-08-23",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_24": {
            "schedule_date": "2020-08-24",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_25": {
            "schedule_date": "2020-08-25",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_26": {
            "schedule_date": "2020-08-26",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_27": {
            "schedule_date": "2020-08-27",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_28": {
            "schedule_date": "2020-08-28",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_29": {
            "schedule_date": "2020-08-29",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_30": {
            "schedule_date": "2020-08-30",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        },
        "2020_08_31": {
            "schedule_date": "2020-08-31",
            "nanny_id": 2,
            "shift_start": "",
            "shift_end": "",
            "schedule_id": 0
        }
    }
}

我有这个 API 响应的数据模型,如下所示:

import Foundation

struct Welcome: Codable {
    let status: Int
    let scheduleData: [String: ScheduleDatum]

    enum CodingKeys: String, CodingKey {
        case status
        case scheduleData = "schedule_data"
    }
}

// MARK: - ScheduleDatum
struct ScheduleDatum: Codable {
    let scheduleDate: String?
    let nannyID: Int?
    let shiftStart: String?
    let shiftEnd: String?
    let scheduleID: Int?

    enum CodingKeys: String, CodingKey {
        case scheduleDate = "schedule_date"
        case nannyID = "nanny_id"
        case shiftStart = "shift_start"
        case shiftEnd = "shift_end"
        case scheduleID = "schedule_id"
    }
}

我想要数据的 schedule_date 值,其中 schedule_id 不是 zero.This schedule_data 是 dictionary.The 的数组 json在 ios swift 代码中以这种方式完成解析。

let decoder = JSONDecoder()
      
      do {
          
          let user = try decoder.decode(ScheduleListModel.self, from: response.data!)
          self.listdata = user
       

       
        if (self.listdata?.scheduleData.count)! > 0
       {

       }
       else
       {
        self.showAlert(message: "No schedule available")
       }
       
         } catch {
             debugPrint(error)
      }

如何获取此数组字典中的值?

您可以根据字典的值过滤字典并获取日期。您应该使用 Welcome 模型来解析 JSON 并进行过滤。

代码如下:

let decoder = JSONDecoder.init()

let welcomeModel = try! decoder.decode(Welcome.self, from: response.data)

welcomeModel.scheduleData.enumerated().forEach {
    if .value.scheduleID != 0 || .value.scheduleID != nil {
        print(.value.scheduleDate ?? "Date is nil")
    } 
}

这里有一些地方需要改进。首先,如果键永远不会变成 nil,则模型属性不必是可选的。其次,不需要编码密钥,因为可以通过 convertFromSnakeCase.

实现相同的目的

型号:

let data = Data(string.utf8)

struct Welcome: Codable {
    let status: Int
    let scheduleData: [String: ScheduleDatum]
}

struct ScheduleDatum: Codable {
    let scheduleDate: String
    let nannyId: Int
    let shiftStart: String
    let shiftEnd: String
    let scheduleId: Int
}

需要对模型进行解码Welcome。此外,循环遍历数据以获得 scheduleDates。方法如下:

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase

    let user = try decoder.decode(Welcome.self, from: response.data)
    var scheduleDates = [String]()

    for schedule in user.scheduleData {
        if schedule.value.scheduleId != 0 {
            scheduleDates.append(schedule.value.scheduleDate)
        }
    }
    print(scheduleDates)
} catch {
    debugPrint(error)
}