更新
我收到错误消息:“错误!无法解码JSON:由于格式不正确,无法读取数据。”。请求中的JSON是
{"page":0,"pageSize":100,"totalPages":1,"numberOfElements":1,"totalElements":1,"hasPreviousPage":false,"hasNextPage":false,"content":[{"id":4554053904,"externalAccountId":null,"source":"PLAN","amount":1073741824,"reportId":null,"rowId":null,"timeCharged":1533427200000,"timeCreated":1533476043000}]}
为什么不起作用? 我不知道如何得到答案...
我的代码和结构:
struct TodoItem: Decodable {
let page: Int?
let pageSize: Int?
let totalPages: Int?
let numberOfElements: Int?
let totalElements: Int?
let hasPreviousPage: Bool
let hasNextPage: Bool
let content: [content]
}
struct content: Decodable {
let id: Int?
let externalAccountId: String?
let source: String?
let amount: Int?
let reportId: Int?
let rowId: Int?
let timeCharged: Int?
let timeCreated: Int?
}
func decodeJson() {
let jsonUrlString = "myurl.com"
print(jsonUrlString)
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (mydata, response, error2) in
guard let datos = mydata else { return }
do {
self.todoList = try JSONDecoder().decode([TodoItem].self, from: datos)
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let jsonError {
print("Error! Could not decode JSON: \(jsonError.localizedDescription)")
}
}.resume()
答案 0 :(得分:1)
该问题与标题无关。
无论如何。您的JSON不是TodoItem的数组。
答案 1 :(得分:0)
1-加载完成后应缓存数据,以免再次滚动到该单元格时重新加载
2-加载单元格0-> 10->的数据时可以显示全屏加载,并在完成所有单元格后重新加载tableview
3-您可以从服务器进行更新,而无需在应用程序中进行更新,只需一次请求即可加载许多项目
希望对您有用