I am parsing Json with Alamofire and SwiftyJSON. I can see all json data from url. Everything is clear in my console but I need to append some values in string arrays. As you can see json values. I create 3 string arrays. I need to append to arrays teams, player names and trophies. I will list team and player name in tableview. I need help to append values in arrays
// json values in console
[
{
"id" : 1,
"team" : "Liverpool",
"players" : [
{
"id" : 2,
"name" : "Alisson",
"position" : "Goal Keeper",
"number" : "13"
},
{
"id" : 3,
"name" : "Salah",
"position" : "Forward",
"number" : "10"
}
],
"trophies" : [
"2019 champions league",
"2005 champions league"
],
"logoUrl" : "url"
},
{
"id" : 4,
"team" : "Real Madrid",
"players" : [
{
"id" : 5,
"name" : "Ramos",
"position" : "Defender",
"number" : "4"
},
{
"id" : 6,
"name" : "Benzema",
"position" : "Forward",
"number" : "9"
}
],
"trophies" : [
"2018 champions league",
"2017 champions league",
"2016 champions league"
],
"logoUrl" : "url"
}
]
import Alamofire
import SwiftyJSON
var team = [String]()
var playerName = [String]()
var trophies = [String]()
func fetchJsonData(){
DispatchQueue.main.async {
Alamofire.request(url).responseData { response in
guard let data = response.data else { return }
do {
let res = try JSONDecoder().decode([PageData].self, from:data)
print(res)
} catch {
print("having trouble converting it to a dictionary" , error)
}
}
}
}
// this is my modal file
struct PageData: Codable {
let team: String?
let players: [Player]
let trophies: [String]
let logoUrlL: String?
}
struct Player: Codable {
let id: Int
let name,position, number: String?
}
You need to do
1- Declare an array like
2- Assign here and reload
3- Set dataSource and delegate
Edit:
First make
res
mutableAdd a player to Liverpool
Add a trophy to Real Madrid
Add a team
And according to your code you don't use
SwiftyJSON