Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Json decode result is printing into console in swift playground

I am new to swift . I created simple playground and added the file with extension json into playground . I am trying to decode the result and print the ID into console but any reason ,it not printing the result into console , I do not see error into console window ..

Here is the playground project structure ..
enter image description here

Here is my json file ..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let json = """
{
 "id": "1",
 "options": [
 {
 "id": "11",
 "options": [
 {
 "id": "111",
 "options": []
 }
 ]
 },
 {
 "id": "2",
 "options": [
 {
 "id": "21",
 "options": []
 },
 {
 "id": "22",
 "options": [
 {
 "id": "221",
 "options": []
 }
 ]
 }
 ]
 }
 ]
}

Here is the code .. I tried ..

struct People: Codable {
    let id: String
    let options: [People]
}
func loadJson(filename fileName: String) -> People? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(People.self, from: data)
            print(jsonData.id)
            return jsonData
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

It not printing the ID of the decode json ..

>Solution :

So I did get it to print the ID, I changed the people file name to people.json and changed the contents to:

{
    "id": "1",
    "options": [{
            "id": "11",
            "options": [{
                "id": "111",
                "options": []
            }]
        },
        {
            "id": "2",
            "options": [{
                    "id": "21",
                    "options": []
                },
                {
                    "id": "22",
                    "options": [{
                        "id": "221",
                        "options": []
                    }]
                }
            ]
        }
    ]
}

(Notice I removed the let json = statement.)

After that in the playground where define the People struct and the loadJson method you can call it like so:

loadJson(filename: "people")

So now you end up with:

struct People: Codable {
    let id: String
    let options: [People]
}


func loadJson(filename fileName: String) -> People? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            let data = try Data(contentsOf: url)
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(People.self, from: data)
            print(jsonData.id)
            return jsonData
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

loadJson(filename: "people")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading