Wie kann ich dieses JSON deserialisieren und die Titel in einem Tableview JSON anzuzeigen: https://www.healthcare.gov/api/articles.jsonDeserialize komplexe JSON mit Alamofire und Swift 3
Was ich bisher versucht:
struct News {
let title : String
init(dictionary: [String:String]) {
self.title = dictionary["mainTitle"] ?? ""
}
}
var newsData = [News]()
func downloadData() {
Alamofire.request("https://www.healthcare.gov/api/articles.json").responseJSON { response in
print(response.request as Any)
print(response.response as Any)
print(response.data as Any)
print(response.result.value)
self.newsData.removeAll()
if let json = response.result.value as? [[String:String]] {
for news in json {
self.newsData.append(News(dictionary: news))
}
self.tableView.reloadData()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
downloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let news = newsData[indexPath.row]
cell.textLabel?.text = news.title
return cell
}
Der Status Server-Code ist 200, also weiß ich, dass meine Anfrage in Ordnung ist. Die Sache ist, dass ich nicht weiß, wie ich ein richtiges Datenmodell schaffen kann.
Was meintest du mit parsedData, es existiert nicht, aber du hast es Artikeln zugewiesen, der Compiler lässt es nicht laufen. –
Durch schlecht, habe ich die Antwort bearbeitet. – vadian
Aber jetzt stürzt meine App ab, weil es eine Null beim Auspacken auf dieser Linie gibt self.newsData.append (Nachrichten (Wörterbuch: Nachrichten als! [String: String])) –