[英]Encodable JSON dictionary in Swift 4
With the help of @matt. 在@matt的帮助下。 I was able to solve it and the finished code is below for anyone who will encounter similer issues in the near future. 我能够解决这个问题,下面的完成代码是任何在不久的将来会遇到类似问题的人的。 Given the below JSON 给定下面的JSON
{
"restaurants": [
{
"id": 1,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 3,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 4,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 5,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 6,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
}
]
}
And I am trying to access the arrays within the Dictionary with this code into a table. 我正在尝试使用此代码将Dictionary中的数组访问到表中。 So I have made a Struct. 所以我做了一个结构。 Below is the content of my Structfile. 以下是我的Structfile的内容。
struct Response: Codable {
let restaurants: [Restaurant]
struct Restaurant: Codable {
let id: Int
let restaurantName: String
let restaurantPicture: String
let status: String
let workingDays: String
let workingHours: String
}
}
This is my main View Controller 这是我的主要View Controller
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tabelView: UITableView!
var restaurants = [Response]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadJSON {
self.tabelView.reloadData()
}
tabelView.delegate = self
tabelView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurants.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = restaurants[indexPath.row].restaurantName.capitalized
return cell
}
func downloadJSON(completed: @escaping ()->()){
let url = URL(string: "http://localhost:8888/tableview/tableView.php")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil{
do{
let downloadedRestaurants = try JSONDecoder().decode(Response.self, from: data!)
print(downloadedRestaurants.restaurants.map{$0.restaurantName})
self.restaurant.append(downloadedRestaurants)
DispatchQueue.main.async {
completed()
}
}catch{
print(error)
}
}
}.resume()
}
}
Thanks @matt. 谢谢@matt。 I hope this piece becomes useful to other newbies or anyone who is having similar issues Cheers! 我希望这篇文章对其他新手或遇到类似问题的人有用!
The problem is that you are trying to decode as [RestaurantStats]
. 问题是您尝试将其解码为[RestaurantStats]
。 But look at your JSON! 但是看看您的JSON! It is not an array of RestaurantStats. 它不是RestaurantStats的数组。 It is not an array at all. 它根本不是数组。 It is a dictionary with a single key, "restaurants"
. 这是一本只有键"restaurants"
的字典。 You need to create that struct and decode as that . 你需要创建一个结构和解码作为 。
EDIT Since I gave my answer, you have revised your code to declare an outer struct Response. 编辑自从我给出答案以来,您已经修改了代码以声明外部结构Response。 And that's correct — and is exactly what I said you should do. 没错-正是我所说的您应该做的。 So decode as Response and you're all set. 因此,将其解码为Response即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.