|
Good morning (o; I've seen many examples where JSONs are fetched and decoded....but all examples I've found are for JSON arrays... But how would I define a Struct and decode a simple JSON dictionary like:
thanks in advance richard |
|
|
|
Apple Developer Decoder Documentation Except yours will be for the
For more general and advances cases of encodable and decodable Apple Developer Codable Documentation |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
If you use Ducky Model Editor it will help on any JSON format. How to use @StewartLynch video Ducky - Model Editor |
|
Just bought Ducky (o; I should check my profile here more often as the forum doesn't send any emails when a reply has been posted... |
|
Hmm..still bugs me this correct initialization.. Ducky converted the JSON fine and spit out:
And works fine with Swift Playground with:
Still have no clue and haven't found an answer how to initialize the Series dictionary correctly.... this just throws an erry that JSON decoder wants to decode an array:
So "series" is initialized as array and not as a dictionary....and all search results about creating an empty dictionary didn't help at all... |
|
Yes, your declaration of You may want to check out the dictionary tutorial / help here or here. To declare an empty dictionary, you will need to pass the types to the dictionary and immediatley create it.
or
In terms of your simple JSON
what roosterBoy wrote, some time ago, is how you would decode that JSON. |
|
Hmm...actually the simple dictionary snippet works fine....just not a dictionary holding an array:
gives:
Either the struct generated from Ducky is not usable here or I have to rewrite the PHP code so it returns just an array instead of an array in a dictionary. But then again that would mean I coudn't use the same JSON API for Swift and for Datatables web applications any longer...or leading to more API calls. Seen that others define separate struct per JSON element like:
But all examples I've found don't use it in a class with ObservableObject where the dictionary has to be initialized. The same struct generated with Ducky works fine with:
Just not with my class: (here the whole snippet)
Maybe the Series struct needs an intializer function for this to work? |
|
Yesssss...that was exactly it (o; Struct looks like this now (thanks to Ducky's initialize option ;o):
And then in my class initialize the dictionary like:
Wonder how complicated it would be with a more complex, nested dictionary (o; |
|
You would write your own macOS helper app. I did, as I found that the available apps were not able to handle more complicated structures. I needed nested arrays of various depths with some elements and sub-arrays being optonal. So my helper app processes TSV files exported from a spreadsheet. As this is a helper app I didn't spend much time developing it. |
|
Well still a couple of those 100 days remaining for me ;-) But I was surprised how fun development with Swift(UI) is.....and this pops up many ideas to implement on macOS/iOS.... after 2 days I was able to deploy a simple iOS app for my girlfriend to check on her iPhone online orders from her online shop when abroad.... I'm glad I haven't bought Xojo few weeks ago....so I'm glad I spent that money on HWS+ today (o; Maybe a good point to list some available macOS helper apps here that people are using for development. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.