Skip to content

Instantly share code, notes, and snippets.

@userow
Created August 23, 2022 22:01
Show Gist options
  • Save userow/68a7b1363e2064bdc1e9dd4ce980ce10 to your computer and use it in GitHub Desktop.
Save userow/68a7b1363e2064bdc1e9dd4ce980ce10 to your computer and use it in GitHub Desktop.
let jsonString =
"""
{"meta":{"code":200,"message":"0K","request_time":1660720775,"limit":4,"total":4,"offset":0,"resource":"https://api.eldorado.ua/v1/promotions_tags?get_promo_by_goods=&goods_ids=1750438&lang=ua&outputPosition=product"},"data":[{"tag_id":"45526344","good_id":"1750438","promotion_id":"7373","tag_name":"+1 \\u0440\\u0456\\u043a \\u0437\\u0430\\u043c\\u0456\\u043d\\u0430 \\u0435\\u043a\\u0440\\u0430\\u043d\\u0443 \\u0434\\u043b\\u044f \\u043d\\u043e\\u0432\\u0438\\u0445 Galaxy","is_promo_code":"0","promo_code_nn":null,"small_description":null,"promotion_algorithm":"16","discount_type":"\\u0414\\u0435\\u043d\\u044c\\u0433\\u0438","promotion_setting_id":"10995","operation_type":"\\u041d\\u0438\\u0447\\u0435\\u0433\\u043e","price_discount":"0","key_set":"0","output_order":"3","image_url":"promotions_tags_images/868-1660308223.svg","image_name":"868","payment_count":"0","priority":"3","title":"1 \\u0440\\u0456\\u043a \\u0437\\u0430\\u043c\\u0456\\u043d\\u0430 \\u0435\\u043a\\u0440\\u0430\\u043d\\u0430","location":"product","tag_type":"regular","dynamic_part_text":null},{"tag_id":"45525781","good_id":"1750438","promotion_id":"433","tag_name":"\\u0414\\u043e 25% \\u0431\\u043e\\u043d\\u0443\\u0441\\u0456\\u0432 \\u0437\\u0430 \\u043f\\u043e\\u043a\\u0443\\u043f\\u043a\\u0438!","is_promo_code":"0","promo_code_nn":null,"small_description":"\\u041f\\u043e\\u043b\\u0443\\u0447\\u0438 \\u0431\\u043e\\u043d\\u0443\\u0441\\u043d\\u044b\\u0435 \\u0433\\u0440\\u0438\\u0432\\u043d\\u044b \\u043d\\u0430 \\u043f\\u0440\\u0438\\u0431\\u044b\\u043b\\u044c\\u043d\\u0443\\u044e \\u043a\\u0430\\u0440\\u0442\\u043e\\u0447\\u043a\\u0443 \\u042d\\u043b\\u044c\\u0434\\u043e\\u0440\\u0430\\u0434\\u043e!","promotion_algorithm":"8","discount_type":"\\u0411\\u043e\\u043d\\u0443\\u0441\\u044b\\u041d\\u0430\\u041f\\u041a","promotion_setting_id":"995","operation_type":"\\u0421\\u043a\\u0438\\u0434\\u043a\\u0430\\u0421\\u0443\\u043c\\u043c\\u0430","price_discount":"3750","key_set":"0","output_order":"100","image_url":"promotions_tags_images/727-1642170508.svg","image_name":"727","payment_count":"0","priority":"100","title":"\\u0434\\u043e 25% \\u0431\\u043e\\u043d\\u0443\\u0441\\u0456\\u0432","location":"product,catalog,promotion,all_promo_page","tag_type":"regular","dynamic_part_text":"\\u0411\\u041e\\u041d\\u0423\\u0421 [\\u0421\\u0443\\u043c\\u043c\\u0430]"},{"tag_id":"45525617","good_id":"1750438","promotion_id":"6599","tag_name":"\\u041d\\u043e\\u0432\\u0456 Galaxy Fold4 | Flip4. \\u041f\\u0435\\u0440\\u0435\\u0434\\u0437\\u0430\\u043c\\u043e\\u0432\\u043b\\u044f\\u0439\\u0442\\u0435 \\u0437 \\u043f\\u043e\\u0434\\u0430\\u0440\\u0443\\u043d\\u043a\\u0430\\u043c\\u0438.","is_promo_code":"0","promo_code_nn":null,"small_description":null,"promotion_algorithm":"16","discount_type":"\\u0414\\u0435\\u043d\\u044c\\u0433\\u0438","promotion_setting_id":"10123","operation_type":"\\u041d\\u0438\\u0447\\u0435\\u0433\\u043e","price_discount":"0","key_set":"0","output_order":"4","image_url":"promotions_tags_images/763-1642170639.svg","image_name":"763","payment_count":"0","priority":"4","title":"\\u041f\\u043e\\u043f\\u0435\\u0440\\u0435\\u0434\\u043d\\u0454 \\u0437\\u0430\\u043c\\u043e\\u0432\\u043b\\u0435\\u043d\\u043d\\u044f","location":"product,catalog,promotion,all_promo_page","tag_type":"regular","dynamic_part_text":null},{"tag_id":"45523343","good_id":"1750438","promotion_id":"4549","tag_name":"\\u041e\\u043f\\u043b\\u0430\\u0442\\u0430 \\u0447\\u0430\\u0441\\u0442\\u0438\\u043d\\u0430\\u043c\\u0438 \\u041f\\u0440\\u0438\\u0432\\u0430\\u0442\\u0411\\u0430\\u043d\\u043a","is_promo_code":"0","promo_code_nn":null,"small_description":null,"promotion_algorithm":"16","discount_type":"\\u0414\\u0435\\u043d\\u044c\\u0433\\u0438","promotion_setting_id":"8071","operation_type":"\\u041d\\u0438\\u0447\\u0435\\u0433\\u043e","price_discount":"0","key_set":"0","output_order":"3","image_url":"promotions_tags_images/733-1642167505.svg","image_name":"733","payment_count":"0","priority":"3","title":"\\u041f\\u0440\\u0438\\u0432\\u0430\\u0442\\u0411\\u0430\\u043d\\u043a","location":"product,catalog,promotion,all_promo_page","tag_type":"regular","dynamic_part_text":null}]}
"""
class BaseResponseModelCodable: Codable {
var meta: EMBaseMetaResponseModelCodable!
}
class EMBaseMetaResponseModelCodable: Codable {
let code: Int
let message: String
let requestTime: Int
let limit: Int?
let total: Int?
let offset: Int?
let resource: String
enum CodingKeys: String, CodingKey {
case code
case message
case requestTime
case limit
case total
case offset
case resource
}
}
class ProductPromo: Codable {
let outputOrder: String
let text: String
enum CodingKeys: String, CodingKey {
case text = "dynamic_part_text"
case outputOrder = "output_order"
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.outputOrder = (try? container.decode(String.self, forKey: .outputOrder)) ?? ""
self.text = (try? container.decode(String.self, forKey: .text)) ?? ""
}
}
class ProductDataPromoCodable: BaseResponseModelCodable {
let data: [ProductPromo]
enum CodingKeys: String, CodingKey {
case data
}
required init(from decoder: Decoder) throws{
let container = try decoder.container(keyedBy: CodingKeys.self)
self.data = (try? container.decode([ProductPromo].self, forKey: .data)) ?? []
try super.init(from: decoder)
}
}
if let jsonData = jsonString.data(using: String.Encoding.utf8) {
print(jsonData)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let productPromo = try! decoder.decode(ProductDataPromoCodable.self, from: jsonData)
print(productPromo.data)
print(productPromo.meta as Any)
print(productPromo.meta.code ?? "no code")
print(productPromo.meta.requestTime ?? "no code")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment