Swift : 无法读取数据,因为它的格式不正确
Swift : The data couldn’t be read because it isn’t in the correct format
我尝试使用 Alamofire 调用 POST Api,但它向我显示格式不正确的错误。
这是我的 JSON 回复:
[
{
"_source": {
"nome": "LOTERIAS BELEM",
"endereco": "R DO COMERCIO, 279",
"uf": "AL",
"cidade": "BELEM",
"bairro": "CENTRO"
},
"_id": "010177175"
},
{
"_source": {
"nome": "Bel Loterias"
},
"_id": "80224903"
},
{
"_source": {
"nome": "BELLEZA LOTERIAS",
"endereco": "R RIVADAVIA CORREA, 498",
"uf": "RS",
"cidade": "SANTANA DO LIVRAMENTO",
"bairro": "CENTRO"
},
"_id": "180124986"
}
]
class Album: Codable {
var _source : [_source]
}
class _source: Codable {
var nome : String
var endereco : String
var uf : String
var cidade : String
var bairro : String
}
var arrList = [Album]()
这就是我尝试使用 Alamofire 解码的方式。
func request() {
let urlString = URL(string: "My Url")
// Alamofire.request(url!).responseJSON {(response) in
Alamofire.request(urlString!, method: .post, parameters: ["name": "belem"],encoding: JSONEncoding.default, headers: nil).responseJSON {
(response) in
switch (response.result) {
case .success:
if let data = response.data {
do {
let response = try JSONDecoder().decode([Album].self, from: data)
DispatchQueue.main.async {
self.arrList = response
}
}
catch {
print(error.localizedDescription)
}
}
case .failure( let error):
print(error)
}
}
}
我想推荐你使用 json4swift.com。您只需复制 json 并粘贴到那里。它将自动从您的 json.
创建模态结构或 class
回到您的问题,您的 class 相册没有 [_source] 数组。这就是您收到以下错误 "The data couldn’t be read because it isn’t in the correct format".
的原因
试试下面给定的相册格式 class,
class Album: Codable
{
var source: Source?
var id: String?
}
请尽量避免在Swift中使用下划线。
只是你的 Album
模型不正确。
struct Album: Codable {
var source : Source
var id : String
enum CodingKeys: String, CodingKey {
case source = "_source"
case id = "_id"
}
}
struct Source: Codable {
var nome : String
var endereco : String?
var uf : String?
var cidade : String?
var bairro : String?
}
如果您不想要 _id
,只需删除相关部分即可。
至于你的Alamofire
相关代码,那部分很好。
显着改进:
- 通过自定义
CodingKeys
用于键映射目的,避免了模型中带有下划线的变量名称
- 类型名称应始终以大写字母开头(因此
_source
是 Source
)
- 同样,变量名应始终以小写字母开头
- 将一些变量设为可选(根据您更新后的回复)
- 保持变量非可选意味着它必须出现在要创建模型的响应中
- 使变量成为可选意味着响应中可能存在也可能不存在键,并且它不存在不会阻止创建模型
我尝试使用 Alamofire 调用 POST Api,但它向我显示格式不正确的错误。
这是我的 JSON 回复:
[
{
"_source": {
"nome": "LOTERIAS BELEM",
"endereco": "R DO COMERCIO, 279",
"uf": "AL",
"cidade": "BELEM",
"bairro": "CENTRO"
},
"_id": "010177175"
},
{
"_source": {
"nome": "Bel Loterias"
},
"_id": "80224903"
},
{
"_source": {
"nome": "BELLEZA LOTERIAS",
"endereco": "R RIVADAVIA CORREA, 498",
"uf": "RS",
"cidade": "SANTANA DO LIVRAMENTO",
"bairro": "CENTRO"
},
"_id": "180124986"
}
]
class Album: Codable {
var _source : [_source]
}
class _source: Codable {
var nome : String
var endereco : String
var uf : String
var cidade : String
var bairro : String
}
var arrList = [Album]()
这就是我尝试使用 Alamofire 解码的方式。
func request() {
let urlString = URL(string: "My Url")
// Alamofire.request(url!).responseJSON {(response) in
Alamofire.request(urlString!, method: .post, parameters: ["name": "belem"],encoding: JSONEncoding.default, headers: nil).responseJSON {
(response) in
switch (response.result) {
case .success:
if let data = response.data {
do {
let response = try JSONDecoder().decode([Album].self, from: data)
DispatchQueue.main.async {
self.arrList = response
}
}
catch {
print(error.localizedDescription)
}
}
case .failure( let error):
print(error)
}
}
}
我想推荐你使用 json4swift.com。您只需复制 json 并粘贴到那里。它将自动从您的 json.
创建模态结构或 class回到您的问题,您的 class 相册没有 [_source] 数组。这就是您收到以下错误 "The data couldn’t be read because it isn’t in the correct format".
的原因试试下面给定的相册格式 class,
class Album: Codable
{
var source: Source?
var id: String?
}
请尽量避免在Swift中使用下划线。
只是你的 Album
模型不正确。
struct Album: Codable {
var source : Source
var id : String
enum CodingKeys: String, CodingKey {
case source = "_source"
case id = "_id"
}
}
struct Source: Codable {
var nome : String
var endereco : String?
var uf : String?
var cidade : String?
var bairro : String?
}
如果您不想要 _id
,只需删除相关部分即可。
至于你的Alamofire
相关代码,那部分很好。
显着改进:
- 通过自定义
CodingKeys
用于键映射目的,避免了模型中带有下划线的变量名称 - 类型名称应始终以大写字母开头(因此
_source
是Source
)- 同样,变量名应始终以小写字母开头
- 将一些变量设为可选(根据您更新后的回复)
- 保持变量非可选意味着它必须出现在要创建模型的响应中
- 使变量成为可选意味着响应中可能存在也可能不存在键,并且它不存在不会阻止创建模型