我正在尝试将json输出传递给label,但我无法获取json的字符串,如何在视图控制器中获取名称? 输出,我将只获得1记录
var results = []
override func viewDidLoad() {
super.viewDidLoad()
self.loadSites()
//self.labelName.text = results["name"].string!
}
func loadSites(){
Alamofire.request(.POST, "https://exapmle.com/api")
.responseJSON { (request, response, json, error) in
if(json != nil){
var jsonObj = JSON(json!)
println(jsonObj)
}
}
}
JSON输出
{
"status" : "success",
"result" : [{"name" : "Example"}]
}
答案 0 :(得分:1)
jsonResult = JSON(json!)
self.labelName.text = jsonResult["result"][0]["name"].string!
答案 1 :(得分:0)
您必须更新主线程中的UI。这就是我在label.text
内更新dispatch_async(dispatch_get_main_queue(),{})
的原因。
func loadSites(){
Alamofire.request(.POST, "https://exapmle.com/api")
.responseJSON { (request, response, json, error) in
if(json != nil) {
var jsonObj = JSON(json!)
println(jsonObj)
if let name = jsonObj["result"][0]["name"].string {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = name
})
}
}
}
}