从 HTTP JSON 树接收特定信息
Receive specific information from HTTP JSON Tree
我怎样才能得到它以便我可以收到特定信息,例如 "username" 并将其放在 userName.text 标签上。 (我输入的 Google.com 字符串只是为了简单起见。)
我一直无法找到完全适合我的问题的解决方案。谢谢!
下面是我class中的代码:
@IBOutlet weak var myData: UITextView!
@IBOutlet weak var userName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main) {
(response, data, error) in
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
...
你可以试试
let url = String(format: "your url")
guard let serviceUrl = URL(string: url) else { return }
var request = URLRequest(url: serviceUrl)
request.httpMethod = "GET"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String:[[String:String]]]
let arr = json["data"]!
print(arr.first!["username"])
print(arr.first!["password"])
} catch {
print(error)
}
}
}.resume()
或更好
struct Root: Codable {
let data: [Datum]
}
struct Datum: Codable {
let username, password: String
}
有
let json = try JSONDecoder().decode(Root.self,from: data)
print(json.data.first?.usernamae)
print(json.data.first?.password)
我怎样才能得到它以便我可以收到特定信息,例如 "username" 并将其放在 userName.text 标签上。 (我输入的 Google.com 字符串只是为了简单起见。)
我一直无法找到完全适合我的问题的解决方案。谢谢!
下面是我class中的代码:
@IBOutlet weak var myData: UITextView!
@IBOutlet weak var userName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main) {
(response, data, error) in
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
...
你可以试试
let url = String(format: "your url")
guard let serviceUrl = URL(string: url) else { return }
var request = URLRequest(url: serviceUrl)
request.httpMethod = "GET"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String:[[String:String]]]
let arr = json["data"]!
print(arr.first!["username"])
print(arr.first!["password"])
} catch {
print(error)
}
}
}.resume()
或更好
struct Root: Codable {
let data: [Datum]
}
struct Datum: Codable {
let username, password: String
}
有
let json = try JSONDecoder().decode(Root.self,from: data)
print(json.data.first?.usernamae)
print(json.data.first?.password)