reddit에서 일부 Json 데이터를 구문 분석하고 테이블보기에 정보를 표시하는 방법을 찾으려고합니다. (https://api.reddit.com). 는 지금까지 내 코드는 모습입니다 :Reddit : 신속한 JSON 구문 분석 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
}
이 정보가 실제로 "redditALL"을 통해 오는 일정하지만 난 내가 JSONSerialization 후 잘못된 뭘하는지 모르겠어요 . 또한 JSON 구문 분석을 신속하게 이해하는 데 도움이되는 링크가 있으면 감사하겠습니다. 감사합니다.
json 데이터 구조를 표시 할 수 있습니까? –