带有Codable返回数组的Swift Rest API调用 [英] Swift Rest API Call with Codable returning array
问题描述
这是此问题的进一步链接使用Codable的Swift Rest API调用示例
This is a further link on this question Swift Rest API call example using Codable
当我更改代码以处理数组时,出现JSON错误.这就是我在做什么.我究竟做错了什么?错误为"json错误".
When I change the code to handle an array, I'm getting a JSON error. Here's what I'm doing. What am I doing wrong? It errors with "error with json".
// Transaction Class
import Foundation
final class Transaction: Codable {
var id: Int?
var date: Date
var amount: Int
var planeID: Int
var userID: UUID
var month: Int
var year: Int
init(date: Date, amount: Int, planeID: Int, userID: UUID, month: Int, year: Int) {
self.date = date
self.amount = amount
self.planeID = planeID
self.userID = userID
self.month = month
self.year = year
}
}
// Call in my ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getJson() { (json) in
print(json.count)
//print(json[0].note)
print(json)
}
}
func getJson(completion: @escaping ([Transaction])-> ()) {
let urlString = "http://192.168.1.98:8080/admin/transactions"
if let url = URL(string: urlString) {
URLSession.shared.dataTask(with: url) {data, res, err in
guard let data = data else {return print("error with data")}
let decoder = JSONDecoder()
guard let json = try? decoder.decode([Transaction].self, from: data) else {return print("error with json")}
completion(json)
}.resume()
}
}
}
这是我在Rested中调用时的JSON响应正文
And this is the JSON response body when I call in Rested
//Form of the JSON response body when I used call via Rested
[
{
"amount": 1000,
"userID": "7698E011-6643-421E-A30D-121FF488FDBB",
"id": 2,
"month": 5,
"date": "2021-01-14T20:30:31Z",
"year": 2021,
"planeID": 1
},
{
"amount": 1000,
"userID": "7698E011-6643-421E-A30D-121FF488FDBB",
"id": 3,
"month": 5,
"date": "2021-01-14T20:30:31Z",
"year": 2021,
"planeID": 2
},
{
"amount": 1000,
"userID": "7698E011-6643-421E-A30D-121FF488FDBB",
"id": 4,
"month": 5,
"date": "2021-01-14T20:30:31Z",
"year": 2021,
"planeID": 2
},
{
"amount": 1000,
"userID": "7698E011-6643-421E-A30D-121FF488FDBB",
"id": 5,
"month": 5,
"date": "2021-01-14T20:30:31Z",
"year": 2021,
"planeID": 2
},
{
"amount": 1000,
"userID": "7698E011-6643-421E-A30D-121FF488FDBB",
"id": 6,
"month": 5,
"date": "2021-01-14T20:30:31Z",
"year": 2021,
"planeID": 2
}
]
推荐答案
如果使用 try
/ catch
并打印错误,则会看到错误发生解码 date
属性时.(如评论中所述,不要用 try?
隐藏错误-您不会知道出了什么问题)
If you use try
/catch
and print the error, you'd see that the error happens when decoding the date
property. (as mentioned in comments, don't hide the error with try?
- you won't know what went wrong)
要将日期正确解码为 Date
,您需要在解码器上设置日期解码策略.在您的情况下,日期似乎是标准的ISO8601,这是 JSONDecoder
中的内置解码策略.因此,您只需要添加一行即可:
To decode the date correctly into Date
, you need to set a date decoding strategy on the decoder. In your case, the date appears to be a standard ISO8601, which is a built-in decoding strategy in JSONDecoder
. So, all you need to do is just add one line:
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601 // <-- ADD THIS
// use do/try/catch with decoding
do {
let transactions = try decoder.decode([Transaction].self, from: data)
completion(transactions)
} catch {
print(error) // or handle it somehow
}
这篇关于带有Codable返回数组的Swift Rest API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!