1

I have just been introduced to using JSON data in my iOS app. I used a service to parse JSON data from a website, and I would like to decode that data to display to my user in a UITableView. My JSON data looks as follows:

{
  "success": true,
  "outputScenario": "Default",
  "data": {
    "collection": [
      {
        "teamName": "Gulf Coast Monarchs, FL",
        "teamW": "10",
        "teamL": "0",
        "teamT": "0",
        "teamPct": "1.000",
        "teamGB": "-",
        "teamGP": "10",
        "teamRA": "10",
        "teamDivision": "10-0-0"
      },
      {
        "teamName": "Ohio Nationals, OH",
        "teamW": "9",
        "teamL": "1",
        "teamT": "0",
        "teamPct": ".900",
        "teamGB": "1.0",
        "teamGP": "10",
        "teamRA": "20",
        "teamDivision": "9-1-0"
      },      {
        "teamName": "Mount Kisco Chiefs, NY",
        "teamW": "0",
        "teamL": "8",
        "teamT": "0",
        "teamPct": ".000",
        "teamGB": "8.0",
        "teamGP": "8",
        "teamRA": "108",
        "teamDivision": "0-8-0"
      }
      {
        "teamName": "Mount Kisco Chiefs, NY",
        "teamW": "0",
        "teamL": "8",
        "teamT": "0",
        "teamPct": ".000",
        "teamGB": "8.0",
        "teamGP": "8",
        "teamRA": "108",
        "teamDivision": "0-8-0"
      }
    ]
  },

Just keep in mind that I have cut out a significant amount of the data provided in the JSON so it is easily viewable.

I would like to decode this data using SwiftyJSON if possible so I can display it to my user in a UITableView. For now, the UITableView will display the team name in the UITableView.textLabel.text and the teamW and teamL in the UITableView.detailTextLabel.text. How would I decode this data using SwiftyJSON? I am struggling to figure out how this type of structure would be decoded. I would like to use the model that I have created:

struct Standing: Decodable {

var teamName: String
var teamW: Int
var teamL: Int
var teamT: Int
var teamPct: Int
teamGB: Int
teamGP: Int
teamRA: Int
teamDivision: String

}

2 Answers 2

1

Why do you want to use SwiftyJSON as you already adopted Decodable?

The types in your struct are widely wrong because all values are String. And you need two other structs for the parent objects.

struct Root: Decodable {
    let success : Bool
    let outputScenario : String
    let data : TeamData
}

struct TeamData: Decodable {
    let collection : [Standing]
}

struct Standing: Decodable {
    let teamName, teamW, teamL, teamT: String
    let teamPct, teamGB, teamGP, teamRA: String
    let teamDivision: String
}

Decode the data, the Standing array is in the variable standings.

do {
    let result = try JSONDecoder().decode(Root.self, from: data)
    let standings = result.data.collection
} catch {
    print(error)
}
Sign up to request clarification or add additional context in comments.

1 Comment

OK, everything is working perfectly after making those significant changes. I think I was having a hard time understanding the format of the JSON, and my implementation was poor. Thank you for your help.
1

If you're using Decodable, you don't need to use SwiftyJSON at all, everything's built into Swift itself.

Use this as your model struct:

struct Standing: Codable {
    let success: Bool
    let outputScenario: String
    let data: DataClass
}

struct DataClass: Codable {
    let collection: [Collection]
}

struct Collection: Codable {
    let teamName, teamW, teamL, teamT: String
    let teamPct, teamGB, teamGP, teamRA: String
    let teamDivision: String
}

and parse it like so:

do {
  let standing = try JSONDecoder().decode(Standing.self, from: data)
} catch {
  print(error)
}

1 Comment

Thank you for your response. Along with vadian's answer, I was able to properly structure my code. Everything is working as it should now.

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.