I am very new to swift.
I need to create the below JSON format
Currently I am using enum to convert my data to JSON format. The issue with this is that my blood pressure has different keys (systolic and diastolic). Also I need to display the key names like "Items", "BloodPressure", "HearRate".
How can I achieve my desired JSON format???
I need to create the below JSON format
Code Block { "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" } ] }
Currently I am using enum to convert my data to JSON format. The issue with this is that my blood pressure has different keys (systolic and diastolic). Also I need to display the key names like "Items", "BloodPressure", "HearRate".
How can I achieve my desired JSON format???
Code Block enum HealthDataType: String, Codable { case bloodPressure case heartRate case bmi } struct HealtDataItem: Codable { let endDate: Date let value: Double let startDate: Date let type: HealthDataType } let bloodPressureItem = HealtDataItem(endDate: end, value: bloodPressureValue, startDate: start, type: .bloodPressure) let bmiItem = HealtDataItem(endDate: end, value: bmiValue, startDate: start, type: .bmi) let healthData = [bloodPressureItem, bmiItem] do { let data = try JSONEncoder().encode(healthData) } catch { //error handling }