Parsing JSON Just Became Super Easy in Swift 4 with Decodable

Lets Build That App · 275K views · 2017 · 19:30

Open on YouTube

Software (other)Python, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering or asking how to map JSON onto Swift structs with Decodable and JSONDecoder.

Recommended by 4 people across 4 different months, 2017 to 2019.

What it is

A 19-minute iOS tutorial from the Lets Build That App channel that shows how to turn JSON into model objects in Swift 4. It first walks through the old JSONSerialization approach, then replaces it with Decodable and JSONDecoder, including arrays and missing fields.

What people said

Recommends HongKongTom · 2017-08 · Stack Overflow · on “json swift4 how to set the struct?”

You are right. So my mistake was not to set the enum "either" and let run it through each key, each by each. And that s because decode can only work with int and string automatically and no array. I just try to understand where is my mistake of thoughts. If you watch that video it look so easy [this video] and even to do without the init stuff.

Recommends Rajan Maheshwari · 2017-10 · Stack Overflow · on “what is wrong in the getting json in swift 4?”

@SaeedRahmatolahi Why have you used `init` in your struct if you are using the `JSONDecoder()` as Swift 4 maps like an object mapper library itself. For the second part of your answer where there is an array, you can watch this video [this video] to understand how mapping works.

Recommends Hardik Modha · 2018-10 · Stack Overflow · on “Parse JSON with similar elements using SwiftyJSON”

You should use Alamofire with JSONDecoder instead swiftyjson and more reference you can watch this video which is available on this link [this video]

Recommends Basel · 2019-02 · Stack Overflow · on “How to execute codable values within viewDidload and Search option by name field using swift 4.2?”

You need to create a new variable of type Root for example var root : Root and on viewDidLoad you make the request (decode) depend if it's from API or local JSON file. and then the result from the decode will save inside root variable root = result then you can access root.cost I also recommend you to see this video [this video] it will help you understand how to use decodable

Cites Koh · 2017-12 · Stack Overflow · on “Decoding JSON with varying key-value pair using Decodable”

I am attempting to parse JSON using Decodable that has the following structure and I am being thrown the error: does not confirm to protocol "Decodable" The JSON structure looks as such: { base = SGD; date = "2017-12-29"; rates = { AUD = "0.95769"; BGN = "1.2205"; THB = "24.414"; TRY = "2.8372"; USD = "0.74844"; ZAR = "9.2393"; }; } Note that I have shrank the size of the JSON object for readability. The trouble here is that the rates are all different key-value pairs which are unlike the posts here [LINK] and here [LINK] . My code so far as such: struct Fixer: Decodable { let base: String let date: String let rates: [AnyObject] } //at dataTasks do { let results = try JSONDecoder().decode(Fi

Cites J.Doe2 · 2018-04 · Stack Overflow · on “parsing JSON with a decodable?”

I have a JSON file: { "name": "Jens", "time": "11.45", "date": "2018:04:17", "differentTimestamps":[""] "aWholeLotOfnames":{ "name1": "Karl" "name2": "pär" } How to parse above JSON ? I have checked this tutorial [this video] . One text tutorial to but i don't get how to make a variable that can take a "nameOfVar"{} If it's not a dictionary. The tutorial are using a var nameOfVar : [what should be here in this case] for one that nearly looks like it. The thing is though that theirs are starting with a [{ and ends with a }] while mine only starts with a {? i don't know how to solve this?

Cites Leonard · 2018-10 · Stack Overflow · on “Finding documentation on how to import JSON from URL in Swift 4”

I did my research and found a working example [this video] , but I could not find any documentation that explicitly explains importing JSON from URL and decoding it. Perhaps, I am searching with wrong keywords. So, what keywords should use to find out how to import JSON from URL in Swift 4? Are there any well-documented blog post or documentations for it? I went to the Apple's official documentation [LINK] on JSON decoding and it says... import Foundation let data: Data // received from a network request, for example let json = try? JSONSerialization.jsonObject(with: data, options: []) This is not helpful at all! I realized a lot of tutorials skip this step to fetch JSON from URL or just locally