Created
November 29, 2019 16:00
-
-
Save hamada147/a523b85cc0c5682b3d377daa27e4b3a7 to your computer and use it in GitHub Desktop.
Using Swift JSONSerialization along with JSONDecoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct OccType: Codable { | |
var id: Int = 0 | |
var type: String = "" | |
var createdAt: String? = nil | |
var updatedAt: String? = nil | |
var image: String? = nil | |
enum CodingKeys: String, CodingKey { | |
case id, type | |
case createdAt = "created_at" | |
case updatedAt = "updated_at" | |
case image | |
} | |
} | |
struct Details: Codable { | |
var id = 0, userID: Int = 0 | |
var occursOn = "", detailsDescription: String = "" | |
var background: String? = nil | |
var address = "", type = "", createdAt = "", updatedAt: String = "" | |
var isDisabled = 0, isRepeated = 0, allowedGiftsDays: Int = 0 | |
var occType: OccType = OccType() | |
enum CodingKeys: String, CodingKey { | |
case id | |
case userID = "user_id" | |
case occursOn = "occurs_on" | |
case detailsDescription = "description" | |
case background, address, type | |
case createdAt = "created_at" | |
case updatedAt = "updated_at" | |
case isDisabled = "is_disabled" | |
case isRepeated = "is_repeated" | |
case allowedGiftsDays = "allowed_gifts_days" | |
case occType = "occ_type" | |
} | |
} | |
struct Validator: Codable { | |
} | |
struct DataModel: Codable { | |
var id: Int = 0 | |
var type: String = "" | |
var image: String? = nil | |
var remaining: String = "" | |
var details: Details = Details() | |
} | |
struct Response: Codable { | |
var success: Bool = false | |
var data: [String: [DataModel]] = [:] | |
var message: String = "" | |
var validator: Validator = Validator() | |
} | |
var jsonStr = """ | |
{ | |
"success": true, | |
"data": { | |
"Nov 2019": [ | |
{ | |
"id": 35, | |
"type": "Birthday", | |
"image": null, | |
"remaining": "5 days ago", | |
"details": { | |
"id": 35, | |
"user_id": 56, | |
"occurs_on": "2019-11-24 00:00:00", | |
"description": "مناسبة تفوقي", | |
"background": null, | |
"address": "عنوان منزلي قريبا", | |
"type": "1", | |
"created_at": "2019-11-26 16:32:35", | |
"updated_at": "2019-11-26 17:05:42", | |
"is_disabled": 0, | |
"is_repeated": 1, | |
"allowed_gifts_days": 5, | |
"occ_type": { | |
"id": 1, | |
"type": "Birthday", | |
"created_at": null, | |
"updated_at": null, | |
"image": null | |
} | |
} | |
} | |
], | |
"Nov 2020": [ | |
{ | |
"id": 39, | |
"type": "Engagment", | |
"image": null, | |
"remaining": "11 months from now", | |
"details": { | |
"id": 39, | |
"user_id": 56, | |
"occurs_on": "2020-11-10 00:00:00", | |
"description": "Marraige", | |
"background": null, | |
"address": "Cairo", | |
"type": "2", | |
"created_at": "2019-11-29 12:17:12", | |
"updated_at": "2019-11-29 12:17:12", | |
"is_disabled": 0, | |
"is_repeated": 1, | |
"allowed_gifts_days": 10, | |
"occ_type": { | |
"id": 2, | |
"type": "Engagment", | |
"created_at": null, | |
"updated_at": null, | |
"image": null | |
} | |
} | |
}, | |
{ | |
"id": 40, | |
"type": "Marriage", | |
"image": null, | |
"remaining": "11 months from now", | |
"details": { | |
"id": 40, | |
"user_id": 56, | |
"occurs_on": "2020-11-10 00:00:00", | |
"description": "Engaged", | |
"background": null, | |
"address": "Cairo", | |
"type": "3", | |
"created_at": "2019-11-29 12:17:34", | |
"updated_at": "2019-11-29 12:17:34", | |
"is_disabled": 0, | |
"is_repeated": 1, | |
"allowed_gifts_days": 10, | |
"occ_type": { | |
"id": 3, | |
"type": "Marriage", | |
"created_at": null, | |
"updated_at": null, | |
"image": null | |
} | |
} | |
} | |
] | |
}, | |
"message": "Operation done successfully!", | |
"validator": {} | |
} | |
""" | |
func testJSON(jsonStr: String) -> Response? { | |
var response: Response? = nil | |
do { | |
response = Response() | |
if let jsonData = jsonStr.data(using: .utf8) { | |
let JSON = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String: Any] | |
// Simple | |
response!.message = JSON["message"] as? String ?? "" | |
response!.success = JSON["success"] as? Bool ?? false | |
response!.validator = JSON["validator"] as? Validator ?? Validator() | |
// A little bit complicated | |
// Get this JSON object as dictionary (key, value) | |
let data = JSON["data"] as! [String: Any] | |
// go through all the keys of the dictionary ex: (Nov 2019, Nov 2020) | |
for key in data.keys { | |
// print(data[key]) | |
// JSONDecoder needs Data data type so the following line turn it into data | |
let dataData = try! JSONSerialization.data(withJSONObject: data[key]!) | |
// Then we use our normal JSONDecoder to decode the array of models inside of it | |
let decoder = JSONDecoder() | |
let dateModels = try! decoder.decode([DataModel].self, from: dataData) | |
// Finally we set the value in the response | |
response!.data[key] = dateModels | |
} | |
} else { | |
print("Failed: JSON String to Data failed") | |
} | |
} catch let parsingError { | |
print("Error", parsingError) | |
} | |
return response | |
} | |
let response = testJSON(jsonStr: jsonStr) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment