Skip to content

Instantly share code, notes, and snippets.

@d2burke
Last active April 23, 2023 21:11
Show Gist options
  • Save d2burke/d6a527280da2ec5cf4a568dad21444e9 to your computer and use it in GitHub Desktop.
Save d2burke/d6a527280da2ec5cf4a568dad21444e9 to your computer and use it in GitHub Desktop.
struct Profile: Codable {
var skills: [ProfileSkill]
var styles: [ProfileStyle]
var roles: [ProfileRole]
}
struct ProfileSkill: Codable {
var key: String
var label: String
}
struct ProfileStyle: Codable {
var key: String
var label: String
}
struct ProfileRole: Codable {
var key: String
var label: String
}
let json = """
{
"skills": [
{
"key": "PREMIERE",
"label": "Adobe Premiere"
},
{
"key": "AFTER_EFFECTS",
"label": "After Effects"
}
],
"styles": [
{
"key": "GAMING",
"label": "Gaming"
},
{
"key": "REACTS",
"label": "Reacts"
}
],
"roles": [
{
"key": "EDITOR",
"label": "Editor"
},
{
"key": "THUMBNAIL_DESIGNER",
"label": "Thumbnail Designer"
}
]
}
"""
let jsonData = json.data(using: .utf8)!
let decoder = JSONDecoder()
let profile = try! decoder.decode(Profile.self, from: jsonData)
print(profile)
@ugiacoman
Copy link

import Foundation

let json = """
{
  "skills": [
    {
      "key": "PREMIERE",
      "label": "Adobe Premiere"
    },
  ],
  "styles": [
    {
      "key": "GAMING",
      "label": "Gaming"
    },
  ],
  "roles": [
    {
      "key": "EDITOR",
      "label": "Editor"
    },
  ]
}
"""

struct ProfileSkill: Codable {
    var key: String
    var label: String
}

struct ProfileStyle: Codable {
    var key: String
    var label: String
}

struct ProfileRole: Codable {
    var key: String
    var label: String
}

struct ProfileResponse: Codable {
    let roles: [ProfileRole]
    let skills: [ProfileSkill]
    let styles: [ProfileStyle]
}

let jsonData = json.data(using: .utf8)!
let profileResponse = try! JSONDecoder().decode(ProfileResponse.self, from: jsonData)

print(dump(profileResponse))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment