Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

ios - how do I convert my data to JSON format - health app

I am very new to swift.
I need to create the below JSON format

{   "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???

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
}
question from:https://stackoverflow.com/questions/65895854/how-do-i-convert-my-data-to-json-format-health-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...