Reputation: 1
How to retrieve information in a json with number elements ? I have to take informations in a Json in swift, but i have elements who are numbers and i don't know how to take the informations of these parts.
In my Json I managed to recover some information, no problems. But those who have a title consists of a number I can not because I can not declare a variable "1" for example. I take the element title in day , but i don't know how to take the informations points in 1 in rankings
The url of the Json is: https://www.lnr.fr/flux/rankings?id_competition=prod2
class Classification: Codable {
let channel : Channel
let classification : [Day]
init(channel: Channel ,classification: [Day] ) {
self.channel = channel
self.classification = classification
}
}
class Channel: Codable {
let title: String
init (title: String) {
self.title = title
}
}
class Day: Codable {
let day: day
init(day: day) {
self.day = day
}
}
class day: Codable {
let title: String
let rankings : Equipe
init(title: String, rankings: Equipe) {
self.title = title
self.rankings = rankings
}
}
class Equipe: Codable {
let club: Club
let difference: String
let points: String
let points_marques: String
let points_encaisses: String
let nombre_matchs_joues: String
let nombre_matchs_gagnes: String
let nombre_matchs_nuls: String
let nombre_matchs_perdus: String
init(club: Club,difference: String, points: String, points_marques: String, points_encaisses: String, nombre_matchs_joues: String, nombre_matchs_gagnes: String, nombre_matchs_nuls: String, nombre_matchs_perdus: String) {
self.club = club
self.difference = difference
self.points = points
self.points_marques = points_marques
self.points_encaisses = points_encaisses
self.nombre_matchs_joues = nombre_matchs_joues
self.nombre_matchs_gagnes = nombre_matchs_gagnes
self.nombre_matchs_nuls = nombre_matchs_nuls
self.nombre_matchs_perdus = nombre_matchs_perdus
}
}
class Club: Codable {
let name: String
let url: logo
init(name: String, url: logo) {
self.name = name
self.url = url
}
}
class logo: Codable {
let url: String
init(url: String) {
self.url = url
}
}
Upvotes: 0
Views: 89
Reputation: 2780
You can use custom CodingKeys for that; checkout Encoding and Decoding Custom Types
Example:
struct Foo: Codable {
var bar: String
var baz: Int
enum CodingKeys: String, CodingKey {
case bar = "1"
case baz = "2"
}
}
let json = "{\"1\":\"bar\",\"2\":\"baz\"}".data(using: .utf8)
let decoder = JSONDecoder()
let foo = try decoder.decode(Foo.self, from: json)
// foo.bar = 'bar'
// foo.baz = 'baz'
// this also works with encoding
let foo = Foo(bar: "MyBar", baz: "MyBaz")
// foo encodes to {"1":"MyBar", "2":"MyBaz"}
Nested example struct FooBar: Codable { var foo: Foo var bar: String var baz: Int }
struct Foo: Codable {
var bar1: String
var bar2: String
var bar3: String
var bar4: String
var bar5: String
var bar6: String
enum CodingKeys: String, CodingKey {
case bar1 = "1"
case bar2 = "2"
case bar3 = "3"
case bar4 = "4"
case bar5 = "5"
case bar6 = "6"
}
}
This is the same as the json structure:
{
"foo": {
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four",
"5": "Five",
"6": "Six"
},
"bar": "Barrr",
"baz": "Bazz"
}
Upvotes: 2
Reputation: 517
You can use the CodingKeys
Example:
struct MyModel: Codable {
let first: String
enum CodingKeys: String, CodingKey {
case first = "1"
}
}
Upvotes: 1