Yasser
Yasser

Reputation: 263

Parsing JSON response into a struct

I'm using Alamofire and getting back the data from the API in JSON. i want to use the data into a struct. the response is the following:

{
  "status" : 1,
  "data" : [
{
  "classes" : [

  ],
  "day" : "Wednesday"
},
{
  "classes" : [
    {
      "time" : "1:30 PM",
      "available_spots" : 2,
      "trainer" : "Ahmed Jasim",
      "thumbnail" : "\/uploads\/classes\/6\/files\/class_6_15555066955cb72607131886.71740331.jpg",
      "id" : 6,
      "name" : "Gym Class C"
    }
  ],
  "day" : "Thursday"
},
{
  "classes" : [

  ],
  "day" : "Friday"
},
{
  "classes" : [

  ],
  "day" : "Saturday"
},
{
  "classes" : [
    {
      "available_spots" : 2,
      "id" : 1,
      "trainer" : "Ahmed Jasim",
      "name" : "Karate",
      "thumbnail" : "\/uploads\/classes\/1\/files\/class_1_15548800215cad9615e62404.41767774.jpg",
      "time" : "5:00 PM"
    }
  ],
  "day" : "Sunday"
},
{
  "classes" : [
    {
      "thumbnail" : "\/uploads\/classes\/1\/files\/class_1_15548800215cad9615e62404.41767774.jpg",
      "available_spots" : 2,
      "time" : "11:00 AM",
      "name" : "Karate",
      "id" : 1,
      "trainer" : "Ahmed Jasim"
    },
    {
      "trainer" : "Jalal Hassan",
      "available_spots" : 20,
      "thumbnail" : "\/uploads\/classes\/2\/files\/class_2_15548819805cad9dbc7b9a80.11639647.jpg",
      "time" : "4:00 PM",
      "name" : "Gym Class B",
      "id" : 2
    }
  ],
  "day" : "Monday"
},
{
  "classes" : [
    {
      "name" : "Gym Class B",
      "trainer" : "Jalal Hassan",
      "time" : "9:00 AM",
      "available_spots" : 20,
      "id" : 2,
      "thumbnail" : "\/uploads\/classes\/2\/files\/class_2_15548819805cad9dbc7b9a80.11639647.jpg"
    }
  ],
  "day" : "Tuesday"
}
 ],
 "message" : "Retrieved successfully",
 "success" : true
}

I want to get the ' day & classes ' and use them in a struct. the struct i'm using is as the following.

struct section {
day: String!
classes: [String]! 
}

so i want to get the day and it's classes and store them. the call for the struct:

 var sections = [Section(day: "Monday", classes: ["A","B","C"]]

what i want to achieve is get all the days and it's classes & if the day has no classes it'll just be an empty array.

but i'm not sure if this is possible or not.

this is the code inside Alamofire request:

     let classesJSON: JSON = JSON(response.result.value!)


                if response.data != nil {

                    for index in 0..<classesJSON["data"].count {

                        var sections = Section(day: classesJSON["data"][index]["day"].stringValue, classes: [])
                        for data in classesJSON["data"].arrayValue {

                            for value in data["classes"].arrayValue {
                                sections.classes.append(value["name"].stringValue)
                               //sections.classes[index] = value["name"].stringValue
                            }
                        }
                        print(sections)
                    }

                }

my problem is i'm getting all the classes in the response, what i want is to get the specific class for the day.

Section(day: Optional("Sunday"), classes: Optional(["Gym Class C", "Karate", "Karate", "Gym Class B", "Gym Class B"])

Section(day: Optional("Monday"), classes: Optional(["Gym Class C", "Karate", "Karate", "Gym Class B", "Gym Class B"])

^ this is what i'm getting, but i want to seperate them so i can get the day and it's classes, not all of them

Upvotes: 0

Views: 209

Answers (2)

Gereon
Gereon

Reputation: 17864

I would recommend ditching SwiftyJSON and use real model objects with Codable instead.

Parse your data using structs like these:

struct APIResponse: Codable {
    let status: Int
    let data: [ClassData]
    let message: String
    let success: Bool
}

struct ClassData: Codable {
    let classes: [Class]
    let day: String
}

struct Class: Codable {
    let time: String
    let availableSpots: Int
    let trainer, thumbnail: String
    let id: Int
    let name: String

    enum CodingKeys: String, CodingKey {
        case time
        case availableSpots = "available_spots"
        case trainer, thumbnail, id, name
    }
}

struct Section {
    let day: String
    let classes: [String]
}

do {
    let response = try JSONDecoder().decode(APIResponse.self, from: json)

    let classesPerDay: [String: [Class]] = response.data.reduce(into: [:]) {
        $0[$1.day, default: []].append(contentsOf: $1.classes)
    }

    let sections = classesPerDay.map { Section(day: $0, classes: $1.map { $0.name }) }
    print(sections)
} catch {
    print(error)
}

Upvotes: 2

Muhammad Maaz Ul Haq
Muhammad Maaz Ul Haq

Reputation: 136

You can slightly change your loop

for index in 0..<classesJSON["data"].count {
    var classes: [String] = []
    for item in classesJSON["data"][index]["classes"].arrayValue {
          classes.append(item["name"].stringValue)
    }
    let sections = Section(day: classesJSON["data"][index]["day"].stringValue, classes: classes)
    print(sections)
 }

And make sure you change let to var for both day and classes in your struct

struct Section {
    var day: String
    var classes: [String]
}

Upvotes: 0

Related Questions