-1

How can I create JSON string from other model data?

I am looking to create json string from data but not with success.

Results I am getting in array and then I have used For Loop to pass to AppointmentDownloadModel for json creation.

How can I pass data AppointmentDownloadModel and create JSON String.

Data Receiving Model:

struct ResponseData: Codable {

    let appointments : [Appointment]?
}


let decoder = JSONDecoder()
let response = try decoder.decode(ResponseData.self, from: result! as! Data)
self.appData = response.appointments
for everyApp in self.appData! {

}

Want to pass response data to this model:

struct AppointmentDownloadModel: Codable{
    var appointmentModel: Appointment
    var progress: Int = 0
    var failedList: [Int: String] = [:]
    var isFinished: Bool = false
}

Final need to create this string:

{"appointmentModel":{"appointmentHour":"12:00","backgroundColorDark":"#556f7b","backgroundColorLight":"#cfd8dc","controlHour":"","date":"2020-06-01T12:00:00","heatingType":"b","id":15469622,"isProjectManual":false,"projectFirmName":"Miray Doğalgaz (Erhan Şahin)","projectName":"KÖKSAL ÇOŞKUN-+D1+D3+D5","projectType":"k","subTitle":"18137 902179436-18137-408352"},"failedResourceList":[],"isFinished":false,"progress":0}
Tekhe
  • 149
  • 1
  • 12
  • @vikingosegundo updated with appointment model. – Tekhe Jun 06 '20 at 15:57
  • 1
    Why is everything optional in your structs? Don't make properties optional unless it is really needed. – Joakim Danielson Jun 06 '20 at 16:08
  • @JoakimDanielson actually data comes from back end sometimes properties have values and sometimes not. So thats why I have set them optional. – Tekhe Jun 06 '20 at 16:11
  • Does this answer your question? [How to convert JSON to String in ios Swift?](https://stackoverflow.com/questions/36370541/how-to-convert-json-to-string-in-ios-swift) – Lal Krishna Jun 06 '20 at 16:12

1 Answers1

0

The following decodes the json example and create new AppointmentDownloadModel objects which are then encoded to Data (and converted to a string)

var models = [AppointmentDownloadModel]()

let decoder = JSONDecoder()
do {
    let response = try decoder.decode(ResponseData.self, from: data)
    if let appointments = response.appointments {
        models = appointments.map { AppointmentDownloadModel(appointmentModel: $0)}
    }
} catch {
    print(error)
}

let encoder = JSONEncoder()
do {
    let modelData = try encoder.encode(models)
    let jsonString = String(data: modelData, encoding: .utf8)
    print(jsonString)
} catch {
    print(error)
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Thank you first of all. I have implemented your code. it gives me `print(jsonString)` `[{\"isFinished\":false,\"appointmentModel\":{\"projectName\":\"HASAN ARSLAN\",\"id\":15473662,\"subTitle\":\"52792 \",\"appointmentHour\":\"09:00\",\"projectDistrict\":\"GEBZE\",\"projectFirmName\":\"Dipos 4 Test Firması\",\"date\":\"2020-06-01T09:00:00\",\"backgroundColorLight\":\"#cfd8dc\",\"backgroundColorDark\":\"#556f7b\",\"heatingType\":\"b\",\"isProjectManual\":false,\"controlHour\":\"\",\"projectType\":\"i\"},\"failedList\":{},\"progress\":0}]` – Tekhe Jun 06 '20 at 16:38
  • In my question please check `Final need to create this string:` I need like this how can I achieve that – Tekhe Jun 06 '20 at 16:39
  • You have that string, it's just when it is printed you see all the \ – Joakim Danielson Jun 06 '20 at 16:45