我一直在尝试从交易网站获取数据,使用api:" https://bittrex.com/api/v1.1/public/getmarketsummaries"并需要从这里取出一些值。 我需要一个关于理解JSONarray和Dictionary的指导。 如果代码也可以改进,请。
import UIKit
class DemoJsonTableViewController: UITableViewController {
var listData = [[String : AnyObject]]()
override func viewDidLoad() {
super.viewDidLoad()
let url:String = "https://bittrex.com/api/v1.1/public/getmarketsummaries"
let urlRequest = URL(string: url)
URLSession.shared.dataTask(with: urlRequest!) { (data, response, error) in
if(error != nil){
print(error.debugDescription)
}
else{
do{
self.listData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
{
self.listData = dictionary["result"] as! [[String:AnyObject]]
}
self.tableView.reloadData()
}catch let error as NSError{
print(error)
}
}
}.resume()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.listData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let item = self.listData[indexPath.row]
cell.textLabel?.text = item["MarketName"] as? String
print(self.listData.count)
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
这是我要导入的链接" MarketName"和"最后"进入表格视图,但无法理解JSONarray等。
请,需要帮助。 我是一个非常新的编码所以如果你能纠正代码,它会很友好。 感谢
答案 0 :(得分:0)
将其添加到UITableViewController
:
var listData = [[String : AnyObject]]()
并将您的URLSession
代码替换为:
URLSession.shared.dataTask(with: urlRequest!) { (data, response, error) in
if(error != nil){
print(error.debugDescription)
}
else{
do{
let response = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
self.listData = response["result"] as! [[String:AnyObject]]
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch let error as NSError{
print(error)
}
}
}.resume()
我希望它可以帮到你。