macOS - Getting JSON data from Raw Github + Parsing
It's not that easy for a beginner, you have to load the data asynchronously with URLSession
.
Some of the keys in the JSON dictionaries are missing, therefore you have to declare more struct members as optional.
The types for the keys actions
and warning
are missing, I commented them out, the code works without them, too
struct Mod: Decodable { let id: String let display: String let description: String let url: String? let config: Bool? let enabled: Bool? let hidden: Bool? let icon: String? let categories: [String]? // let actions: Array<OptionAction>? // let warning: ActionWarning?}let url = URL(string: "https://raw.githubusercontent.com/nacrt/SkyblockClient-REPO/main/files/mods.json")!let task = URLSession.shared.dataTask(with: url) { (data, _, error) in if let error = error { print(error); return } do { let result = try JSONDecoder().decode([Mod].self, from: data!) print(result) } catch { print(error) }}task.resume()
There are some bad practices in your code:
- Instead of
try!
use always ado - catch
block if an error could occur. - Variable names in Swift are
lowerCamelCase
rather thansnake_case
. - Don't annotate types the compiler can infer.
- Structs used in an array should be named in singular form (
Mod
) and the JSON suffix is pointless.
Vadian is correct. Whatever is not present in any of the elements of the array must be marked Optional:
struct ModsJSON: Decodable { let id: String let display: String let description: String let url: String? let config: Bool? let enabled: Bool? let hidden: Bool? let icon: String? let categories: Array<String>? // let actions: Array<OptionAction>? //let warning: ActionWarning?}
Your code will then successfully parse the JSON. You can then, as you say, learn all the enabled
values by saying eg.
let enableds = mods_json.map {$0.enabled}
But some of the elements lack the enabled
so you would have to decide how to deal with that. The results appear as
[Optional(true), Optional(true), nil, Optional(true), Optional(true), Optional(true), Optional(true), nil, nil, nil, nil, nil, nil, nil, Optional(false), nil, Optional(false), Optional(false), nil, Optional(false), Optional(false), Optional(false), nil, nil]
How to reflect this data into your interface is a totally different problem!