-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?

  • 2
    Search for "Swift JSON Codable" – jnpdx Jan 29 '22 at 23:20
  • take a look here you will find what you need https://stackoverflow.com/questions/25621120/simple-and-clean-way-to-convert-json-string-to-object-in-swift – belal medhat Jan 29 '22 at 23:21
  • Try pasting exactly what you get from the server – Mr.SwiftOak Jan 29 '22 at 23:26
  • @Mr.SwiftOak I edited my question with the full response – Rohan Mittal Jan 29 '22 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. – user2864740 Jan 29 '22 at 23:36
  • Thanks, although I meant more the structure , not exact names in JSON object. You can replace those with some random strings as you did im original question – Mr.SwiftOak Jan 29 '22 at 23:37

1 Answers1

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 ❌")
        }
    }
}
Mr.SwiftOak
  • 1,469
  • 3
  • 8
  • 19
  • 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? – Rohan Mittal Jan 31 '22 at 19:30
  • 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 – Mr.SwiftOak Jan 31 '22 at 20:02
  • 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 – Mr.SwiftOak Jan 31 '22 at 20:05