The JSON response is:-
{
"id" = "1"
"message" = "SUCCESS"
"data" = "[{"name":"FirstName","office_id":1111,"days_name":"Mon"},
{"name":"SecondName:,"office_id":1112,"days_name":"Tue"}]"
}
I don’t seems to understand how to approach decoding "data", In data model shall the data be declared as String? and I have been trying to figure out but don’t seem to get any clue and stuck here for a while, if anyone can please shed some light to it, it would help a lot. The only problem I am facing is how to deal with "" double quotes wrapped around data array as shown in above response.
Data model and URLSession code is below:
struct Root : Codable {
let id : String?
let message : String?
let data : String?
enum CodingKeys: String, CodingKey {
case id = "id"
case message = "message"
case data = "data"
}
}
struct insideData: Codable {
let name: String?
let officeId : Int?
let daysName: String?
enum CodingKeys: String, CodingKey {
case name = "name"
case officeId = "office_id"
case daysName = "days_name"
}
}
URLSession.shared.dataTask(with: url!) { (responseData, httpUrlResponse , error) in
if(error == nil && responseData != nil && responseData?.count != 0){
let decoder = JSONDecoder()
do{
let result = try decoder.decode(Root.self, from: responseData!)
print(result.data!)
}
catch let error {
debugPrint("Error occured while decoding = (error.localizedDescription)")
}
}
}.resume()
I save result.data! in a new variable and convert it to data and again use JSONDecoder but now with insideData.self struct but don’t get desired output, not getting mapped with keys inside insideData struct.
I am just getting started with learning networking in swift so please pardon me for silly mistakes.
2
Answers
data
value is JSON with JSON, ie it’s a JSONString.A way to parse it is to parse again the JSON String. To do so, you need to override
init(from decoder:)
ofRoot
.Let’s first fix your JSON which isn’t valid, to be able to use it in Playgrounds.
Then, change
data
: let data : [InsideData]You could then:
If you don’t like creating a new decoder, you can pass one in
userInfo
of the decoder:Then the root decoding:
The provided JSON looks to be invalid.
=
sign but the:
sign between a key and value.,
behind each value.:,
should be",
Suggested JSON changes:
I’ve tested decoding this json in Playground and it seems to work:
Print output: