Json is not parsed because of the number in the name

Viewed 49

I can not parse json data due to the fact that the name contains the number 3, not a string Tried it through quicktype.io, but instead of 3 it writes the3, but every time I get nil Tried to create variable 3, but cannot create from number

Here Json

"items": {
        "3": {
            "name": "Тариф Gold 3 месяцев",
            "product_id": 123,
            "taxes": {
                "total": {
                    "1": "0.08"
                },
                "subtotal": {
                    "1": "0.08"
                }
            },
            "meta_data": []
        }
    }

Here is my code

    // MARK: - ServerStatusElement
struct ServerStatusElement: Decodable {
//    var data: DataClass?
    var items: Items?
}

// MARK: - DataClass
struct DataClass: Decodable {
    var id: Int?
    var status: String?
}

struct Items: Codable {
    var items: itemsS
}
struct itemsS: Codable {
    var the3: Int
}
typealias typeNetwork = [ServerStatusElement]
func fetchSubscribe(url: String) {
    let url = URL(string: url)!
    
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let data = data else { return }
        guard error == nil else { return }
        do {
            let value = try JSONDecoder().decode(typeNetwork.self, from: data)


            print(value[0].items)

            } catch let error {
                print("Ошибка: \(error)")
            }
        }.resume()
}
1 Answers

In your struct with the 3 you can add a set of custom coding keys like:


struct itemsS: Codable {
    var the3: Int


    enum CodingKeys: String, CodingKey {
        case the3 = "3"
    }
}
Related