0

I am pretty new to coding so apologies if this should be straightforward for me.

I am using SwiftUI on Xcode, and am using JSON for my data.

I have the JSON file which contains just 7 items: 1 Int and 6 Strings (1 of those an array). I have a model file that maps the data items in JSON to SwiftUI variables. I have a data loader that reads the JSON file and does the correct error checking etc. I have a view that can show the data read from the JSON file.

I need to be able to read the data set for a specific record in the data. When I run my code currently I get the 1st item returned successfully but would now need to amend the code to be able to pull whichever record I require from the dataset.

let questions: [Question] = Bundle.main.decode("questions.json")

then in the body, I use this format to output any data item to the screen (question is one of the variable names defined in the model)

        Text(question.question)

but of course, this only ever returns the first item in the JSON file. How do I read a specific data item please based on one of the items in the JSON file? ...

{
    "id": 2,
    "qn": "1",
    "level": "1",
    "sylabusItem": "1A2",
    "question": "Dummy question 2",
    "answers": ["Dummy answer 1", "Pretend answer 2", "Pretend answer 3", "Pretend answer 4"],
    "correct": "2",
},
{
    "id": 3,
    "qn": "1",
    "level": "1",
    "sylabusItem": "1A2",
    "question": "Dummy question 3",
    "answers": ["Dummy answer 1", "Another answer 2", "Another answer 3", "Another answer 4"],
    "correct": "4",
}

Model file

...

        struct Question: Hashable, Codable, Identifiable {
            var id: Int
            var qn: String
            var level: String
            var sylabusItem: String
            var question: String
            var answers: [String]
            var correct: String


            static let allQuestions: [Question] = Bundle.main.decode("questions.json")
            static let example = allQuestions[0]
        }

Hope I have provided enough info. Thanks all.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
John
  • 99
  • 1
  • 1
  • 10
  • And what do you mean with “ read a specific data item”? Specific as in searching for a question or a specific index or...? Or do you want to iterate over the array? – Joakim Danielson Mar 05 '21 at 10:09
  • The data is a list of questions, with multi choice answers and some other data related to the question etc. I would like to be able to read the 'nth' question set of data out of the JSON file. I also want to be able to iterate over parts of the array. The data will comprise many questions, say 100 in total, with the test comprising 10 questions. Qn 1 will be 1 question selected randomly from the 1st 10, question 2 will again be 1 question selected by the system randomly from 11-20, 3rd question again 1 selected randomly from 21-30 etc. – John Mar 05 '21 at 10:31
  • That's a lot of things you want to do but to get you started look at [this question](https://stackoverflow.com/questions/32773593/generate-array-of-unique-random-numbers-within-inclusive-range) for some ideas on how to generate a unique random number (index) for getting your questions. – Joakim Danielson Mar 05 '21 at 12:01
  • I am able to generate random numbers @JoakimDanielson, it's knowing how to extract specific data out of a JSON file is what I am after. – John Mar 05 '21 at 16:01
  • You shouldn't. You have your array of questions and that is what you should use, `let questions: [Question] = Bundle.main.decode("questions.json")` – Joakim Danielson Mar 05 '21 at 16:02
  • Yep, that's what I am not knowing how to do. Say I want to get item 24 in the data set. How do I do that @JoakimDanielson. I will need to generate a random number each time I wish to extract a random question in the sections from 11-20 or 21-30 etc. – John Mar 05 '21 at 16:05
  • `let question = questions[23]`. To learn more about arrays check out [this chapter](https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html#ID107) in the Swift Programming Language book – Joakim Danielson Mar 05 '21 at 16:08
  • I'm Stil lunsure though @JoakimDanielson how to simply point to any item in the JSON file. All I ca do is return the first item. – John Mar 05 '21 at 16:54

0 Answers0