我正在尝试将检索到的JSON数据显示为UILabel。目前我已在调试器中成功显示数据,但我不确定如何设置相当于UILabel的数据。任何帮助,将不胜感激。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad()
{
super.viewDidLoad()
let url = URL(string: "http://cgi.soic.indiana.edu/~team10/index.php")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
do
{
//Array
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJson)
}
catch
{
}
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您可以尝试以下代码(URLSession dataTask
是let body = try? JSONSerialization.jsonObject(with: data!, options: []) as AnyObject
var jsonString = String()
do {
let jsonData = try JSONSerialization.data(withJSONObject: body, options: .prettyPrinted)
jsonString = String(data: jsonData, encoding: .utf8)!
print("jsonString: \(jsonString)")
} catch let error as NSError {
print(error)
}
DispatchQueue.main.async {
self.sampleLabel.text = jsonString
}
中回调方法的参数):
const time = timeToRedirect || TIME_TO_REDIRECT;