我有这些 JSON 数据。
(
{
email = "b@p.com.my";
login = ID001;
pw = 1234;
},
{
email = "p@d.com.my";
login = ID002;
pw = 12345;
}
)
现在,我只能在 x 代码输出中打印 myJSON 值。
我的问题是,如何将每个 JSON 显示到 UILabel _email、_login、_pw 中?
有人说我需要存储为变量并设置到 UILabel 中,但我不知道该怎么做。如果有人能在这件事上帮助我,我将不胜感激。
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var _email: UILabel!
@IBOutlet var _login: UILabel!
@IBOutlet var _pw: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://localhost/get.php")
let task = URLSession.shared.dataTask(with: url!) {
(data, response, error) in
if error != nil {
print("Error")
return
}
else {
if let content = data {
do {
let myJSON = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJSON)
}
catch {}
}
}
}
task.resume()
}
}
谢谢。
最佳答案
编辑: 在打印 myJSON 的 block 之后,尝试以下操作:
for eachItem in myJSON {
if let emailParsed = eachItem["email"] as? String {
print(emailParsed)
_email.text = emailParsed
}
}
这个循环在所有字典之间运行 <强> This 解决方案可能对您有帮助。请仔细阅读!
关于ios - 如何在 Swift 3.0 中将 JSON 输出设置为 UILabel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627244/