在 Swift 错误中使用 Decodable 解析 JSON
Parsing JSON with Decodable in Swift Error
我想从具有结构和可解码函数的 HTTP 请求中解析一个简短的 JSON 请求。
声明如下:
struct Wert: Codable {
let age: String
let first_name: String
}
let session = URLSession.shared
let url = URL(string: "https://learnappmaking.com/ex/users.json")!
以及我发出请求并尝试解析的代码:
guard let data = data else { return }
do {
let preis = try JSONDecoder().decode(Wert.self, from: data)
print(preis);
}
catch {
print("JSON error: \(error.localizedDescription)")
}
}.resume()
我收到错误:"JSON error: The data couldn’t be read because it isn’t in the correct format." 而且我不知道代码有什么问题
JSON 看起来像:
{
"first_name": "Ford",
"last_name": "Prefect",
"age": 5000
},
{
"first_name": "Zaphod",
"last_name": "Beeblebrox",
"age": 999
},
{
"first_name": "Arthur",
"last_name": "Dent",
"age": 42
},
{
"first_name": "Trillian",
"last_name": "Astra",
"age": 1234
}
]
如果有人能帮助我,那就太好了。
错误:
您使用的JSON无效。有效的 JSON
将是
[{"first_name":"Ford","last_name":"Prefect","age":5000},{"first_name":"Zaphod","last_name":"Beeblebrox","age":999},{"first_name":"Arthur","last_name":"Dent","age":42},{"first_name":"Trillian","last_name":"Astra","age":1234}]
型号:
使用 Int
作为 age
的数据类型,而不是 String
、
struct Wert: Decodable {
let firstName, lastName: String
let age: Int
}
解析:
1.解析时使用[Wert].self
代替Wert.self
,即
2.使用decoder.keyDecodingStrategy = .convertFromSnakeCase
处理JSON中的蛇形(下划线)键, 即
if let url = URL(string: "https://learnappmaking.com/ex/users.json") {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let preis = try decoder.decode([Wert].self, from: data)
print(preis)
} catch {
print(error)
}
}
}.resume()
}
您需要提供自定义键的编码键。
struct Wert: Codable {
let age: String
let firstName: String
enum CodingKeys: String, CodingKey {
case age, firstName = "first_name"
}
}
年龄不是您 Json 文件中的字符串类型,请按以下方式更新您的映射。
struct Wert: Codable {
let age: Int
let first_name: String
}
我想从具有结构和可解码函数的 HTTP 请求中解析一个简短的 JSON 请求。 声明如下:
struct Wert: Codable {
let age: String
let first_name: String
}
let session = URLSession.shared
let url = URL(string: "https://learnappmaking.com/ex/users.json")!
以及我发出请求并尝试解析的代码:
guard let data = data else { return }
do {
let preis = try JSONDecoder().decode(Wert.self, from: data)
print(preis);
}
catch {
print("JSON error: \(error.localizedDescription)")
}
}.resume()
我收到错误:"JSON error: The data couldn’t be read because it isn’t in the correct format." 而且我不知道代码有什么问题
JSON 看起来像:
{
"first_name": "Ford",
"last_name": "Prefect",
"age": 5000
},
{
"first_name": "Zaphod",
"last_name": "Beeblebrox",
"age": 999
},
{
"first_name": "Arthur",
"last_name": "Dent",
"age": 42
},
{
"first_name": "Trillian",
"last_name": "Astra",
"age": 1234
}
]
如果有人能帮助我,那就太好了。
错误:
您使用的JSON无效。有效的 JSON
将是
[{"first_name":"Ford","last_name":"Prefect","age":5000},{"first_name":"Zaphod","last_name":"Beeblebrox","age":999},{"first_name":"Arthur","last_name":"Dent","age":42},{"first_name":"Trillian","last_name":"Astra","age":1234}]
型号:
使用 Int
作为 age
的数据类型,而不是 String
、
struct Wert: Decodable {
let firstName, lastName: String
let age: Int
}
解析:
1.解析时使用[Wert].self
代替Wert.self
,即
2.使用decoder.keyDecodingStrategy = .convertFromSnakeCase
处理JSON中的蛇形(下划线)键, 即
if let url = URL(string: "https://learnappmaking.com/ex/users.json") {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let preis = try decoder.decode([Wert].self, from: data)
print(preis)
} catch {
print(error)
}
}
}.resume()
}
您需要提供自定义键的编码键。
struct Wert: Codable {
let age: String
let firstName: String
enum CodingKeys: String, CodingKey {
case age, firstName = "first_name"
}
}
年龄不是您 Json 文件中的字符串类型,请按以下方式更新您的映射。
struct Wert: Codable {
let age: Int
let first_name: String
}