If you're looking to create JSON models, I recommend using quicktype.io, which can quickly generate the appropriate JSON model classes for you.
I saw that the JSON model you provided in your question was misformatted, so I have fixed the errors, however you should ensure that the attached JSON is identical to what you'd use in your app:
Fixed JSON Model
{ "items" : [
{ "BloodPressure": {
"endDate" : "2020-01-25",
"systolicValue" : "122",
"diastolicValue" : "62",
"startDate" : "2020-01-25"
}
},
{"HeartRate": {
"endDate" : "2020-01-25",
"Value" : "78",
"startDate" : "2020-01-25"
}
},
{ "BMI": {
"endDate" : "2020-01-25",
"Value" : "23",
"startDate" : "2020-01-25"
}
}
]
}
I was able to use the above JSON inside quicktype.io and the result is the model structs you see below. As you can see, the BloodType datatype accounts for both the systolicValue and the diastolicValue. For readability, I've renamed a few of the model structs from the default quicktype.io naming to improve readability within your project.
JSON Model Structs
struct HealthData: Codable {
let metrics: [Metric]
}
// MARK: - Item
struct Metric: Codable {
let bloodPressure: BloodPressure?
let heartRate, bmi: Bmi?
enum CodingKeys: String, CodingKey {
case bloodPressure = "BloodPressure"
case heartRate = "HeartRate"
case bmi = "BMI"
}
}
// MARK: - BloodPressure
struct BloodPressure: Codable {
let endDate, systolicValue, diastolicValue, startDate: String
}
// MARK: - Bmi
struct Bmi: Codable {
let endDate, value, startDate: String
enum CodingKeys: String, CodingKey {
case endDate
case value = "Value"
case startDate
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…