Ich versuche eine Möglichkeit zu finden, einige Json-Daten auf Reddit zu analysieren und die Informationen in einer Tabellenansicht anzuzeigen. (https://api.reddit.com). Bisher ist es das, was mein Code wie folgt aussieht:Reddit: JSON Parsing in swift 3
var names: [String] = []
var comment: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://api.reddit.com")
do{
let reddit = try Data(contentsOf: url!)
let redditAll = try JSONSerialization.jsonObject(with: reddit, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String : AnyObject]
if let theJSON = redditAll["children"] as? [AnyObject]{
for child in 0...theJSON.count-1 {
let redditObject = theJSON[child] as! [String : AnyObject]
names.append(redditObject["name"] as! String)
}
}
print(names)
}
catch{
print(error)
}
}
//Table View
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
//Configure cells...
cell.textLabel?.text = names[indexPath.row]
cell.detailTextLabel?.text = comments[indexPath.row]
return cell
}
ich für eine Tatsache weiß, wird die Information tatsächlich durch den „redditALL“ konstant kommen, aber ich bin nicht sicher, was ich tue falsch nach dem jsonSerialization . Auch würde ich es wirklich schätzen, wenn es eine Art Verbindung gab, um mir zu helfen, JSON Parsing in schnell besserem zu verstehen, Dank.
Können Sie Ihre JSON-Datenstruktur anzeigen? –