-1

In my swift application, I am getting a JSON string from my server side and I am storing it using: let response = String(data: data!, encoding: .utf8)

The string contains and array of dictionaries. It looks like this:

[{"class":"Math","classToken":"SSC000000zctal","teacherName":"Last, First","room":"001","grade":"A+"},{"class":"MUSIC","classToken":"SSC000000zcY2Y","teacherName":"Last,First","room":"002","grade":"A+"}]

Is there a way for me to convert this to an actual array of dictionaries in my swift code?

6
  • 2
    Search for "Swift JSON Codable" Commented Jan 29, 2022 at 23:20
  • take a look here you will find what you need stackoverflow.com/questions/25621120/… Commented Jan 29, 2022 at 23:21
  • Try pasting exactly what you get from the server Commented Jan 29, 2022 at 23:26
  • @Mr.SwiftOak I edited my question with the full response Commented Jan 29, 2022 at 23:32
  • Good. Now take the full response and convert it into a minimal valid representation. For example, the fact that there are 20 (vs an example of say, 2) items in the JSON array is largely irrelevant when determining the mapping structure. Commented Jan 29, 2022 at 23:36

1 Answer 1

0

Here is how you can decode your response:

typealias YourDecodedObject = [ClassObject]

struct ClassObject: Codable {
    let welcomeClass: String?
    let classToken: String?
    let teacherName: String?
    let room: String?
    let grade: String?
    
    enum CodingKeys: String, CodingKey {
        case welcomeClass = "class"
        case classToken, teacherName, room, grade
    }
}

func decodeResponseFrom(data: Data?) {
    if let data = data {
        do {
            let decodedObject = try JSONDecoder().decode(YourDecodedObject.self, from: data)
        } catch {
            print("could not decode ❌")
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for writing this code. I still have two questions: 1. What is the purpose of welcomeClass? 2. What is the use of type alias in this?
When decoding some response from server, we sometimes use CodingKeys, so that keywords in JSON object are not in collision with keywords that Swift uses during compiling. In example your JSON response includes "class" keyword in dictionary, since class cannot be property name (because class is used to create a class) we specify in Codingkeys, that JSON property named "class" would be decoded into properry with some other name - e.g. welcomeClass
2) typealias is basically just placeholder name. We say that an array of [ClassObject] will be called YourDecodedObject . Then we use the YourDecodedObject to specify that we want to decode an array of [ClassObject] from server response

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.