1

Some time ago, I asked a question involving decoding JSON. I am using this JSON to create an entry in a list and decide whether or not to have a checkbox ticked. My previous question asked how to decode the JSON, and I have this code which looks useful (below), but to be honest, I have no idea what to do with.

Where would I put this within a SwiftUI file where I could loop through each piece of JSON in this array and add an entry into a SwiftUI?

I have experience with Python, and I understand how it should be done, but am quite the beginner in swift...

My code:

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 modsURL = 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()

let mods_test: () = importJSON(url: modsURL)
let enableds = mods_test.map {$0.enabled}

If there is anything wrong with this question, please tell me! Thanks in advance for the help.

2

1 Answer 1

2

Here, how you can do that,

you can use State to declare variable

struct ContentView: View {

    //Declare variable
    @State var jsonDataList = [jsonData]()

    var body: some View {
        VStack {
            List(jsonDataList, id: \.id) { jsonDataList in
                VStack(alignment: .leading) {
                    HStack {
                        VStack(alignment: .leading) {
                            Text(jsonDataList.id)
                                .font(.title3)
                                .fontWeight(.bold)
                            Text(jsonDataList.display)
                                .font(.subheadline)
                                .fontWeight(.bold)
                        }
                        Spacer()
                        Image(systemName: jsonDataList.enabled ?? false ? "checkmark.square": "square")
                    }
                }
            }
            .onAppear(perform: loadData)
        }
    }

    //MARK: - Web Service

    func loadData() {

        guard let modsURL = URL(string: "https://raw.githubusercontent.com/nacrt/SkyblockClient-REPO/main/files/mods.json") else {
            print("Invalid URL")
            return
        }

        let task = URLSession.shared.dataTask(with: modsURL) { (data, _, error) in
            if let error = error { print(error); return }
            do {
                let result = try JSONDecoder().decode([jsonData].self, from: data!)
                jsonDataList = result
                print("Response:",jsonDataList)
            } catch {
                print(error)
            }
        }
        task.resume()
    }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
            ContentView()
    }
}

// MARK: - Data Model

struct jsonData: Codable, Identifiable {
    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]?
}

Response Screenshot :

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.