Json 由于名称中的数字而未被解析
Json is not parsed because of the number in the name
我无法解析 json 数据,因为名称包含数字 3,而不是字符串
通过 quicktype.io 尝试过,但它写的不是 3,而是 the3,但每次我都得到 nil
试图创建变量 3,但无法从数字
创建
这里Json
"items": {
"3": {
"name": "Тариф Gold 3 месяцев",
"product_id": 123,
"taxes": {
"total": {
"1": "0.08"
},
"subtotal": {
"1": "0.08"
}
},
"meta_data": []
}
}
这是我的代码
// 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()
}
在带有 3 的结构中,您可以添加一组自定义编码键,例如:
struct itemsS: Codable {
var the3: Int
enum CodingKeys: String, CodingKey {
case the3 = "3"
}
}
我无法解析 json 数据,因为名称包含数字 3,而不是字符串 通过 quicktype.io 尝试过,但它写的不是 3,而是 the3,但每次我都得到 nil 试图创建变量 3,但无法从数字
创建这里Json
"items": {
"3": {
"name": "Тариф Gold 3 месяцев",
"product_id": 123,
"taxes": {
"total": {
"1": "0.08"
},
"subtotal": {
"1": "0.08"
}
},
"meta_data": []
}
}
这是我的代码
// 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()
}
在带有 3 的结构中,您可以添加一组自定义编码键,例如:
struct itemsS: Codable {
var the3: Int
enum CodingKeys: String, CodingKey {
case the3 = "3"
}
}