UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Decode Complicated JSON

Forums > Swift

I have this JSON:

{
    "customers": ["Example", "another"],

    "projects": [
        {
            "project_name": "Website",
            "customer": "another",
            "timer_progress": "0:34:23"
        },
        {
            "project_name": "Logo",
            "customer": "another",
            "timer_progress": "0:58:56"
        },
        {
            "project_name": "Big Project",
            "customer": "another",
            "timer_progress": "5:23:56"
        }
    ]
}

I would like to store it in two Arrays like this:

customers = ["Example", "another"]

projects = [ { "project_name": "Website", "customer": "another", "timer_progress": "0:34:23" }, { "project_name": "Logo", "customer": "another", "timer_progress": "0:58:56" }, { "project_name": "Big Project", "customer": "another", "timer_progress": "5:23:56" } ]

How can I do it? I already tried some things but they don´t work

3      

I am working with SwiftUI

3      

I'm a little unclear about projects. Are you wanting to decode that into an array of [String:String] dictionaries or will you be decoding into an array of model objects?

3      

@roosterboy id like to have it like this:

projects = [ { "project_name": "Website", "customer": "another", "timer_progress": "0:34:23" }, { "project_name": "Logo", "customer": "another", "timer_progress": "0:58:56" }, { "project_name": "Big Project", "customer": "another", "timer_progress": "5:23:56" } ]

an array with dicts

3      

Here's one way to do it...

//get your JSON data from wherever
//for this example, I just used a static string
let json = """
{
    "customers": ["Example", "another"],

    "projects": [
        {
            "project_name": "Website",
            "customer": "another",
            "timer_progress": "0:34:23"
        },
        {
            "project_name": "Logo",
            "customer": "another",
            "timer_progress": "0:58:56"
        },
        {
            "project_name": "Big Project",
            "customer": "another",
            "timer_progress": "5:23:56"
        }
    ]
}
"""

//this is how we tell Swift what format our raw JSON is in
struct DecodedJSON: Decodable {
    let customers: [String]
    let projects: [[String:String]]
}

struct ContentView: View {

    @State private var customers: [String] = []
    @State private var projects: [[String:String]] = []

    var body: some View {
        Button("Load JSON") {
            loadJSON()
        }
        //so we can see that the data loads
        .onChange(of: customers) { _ in
            print(customers)
        }
        .onChange(of: projects) { _ in
            print(projects)
        }
    }

    func loadJSON() {
        //depending on where your JSON is coming from,
        //you should do the necessary set up steps
        guard let data = json.data(using: .utf8) else {
            fatalError("Could not convert JSON string to data")
        }

        //here's where the actual decoding happens
        do {
            let decodedJSON = try JSONDecoder().decode(DecodedJSON.self, from: data)
            customers = decodedJSON.customers
            projects = decodedJSON.projects
        }
        catch {
            print(error)
        }
    }
}

3      

ok thanks

i try it tomorrow (europe)

3      

Thank You

It works

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.