我发出了此请求:
func AlamofireGetCode()
{
let username: String = searchTextField.text!
var url:String!
url = "https:// ... "
AF.request(url, method: .get, encoding: JSONEncoding.default)
.responseJSON { response in
switch response.result {
case .success:
debugPrint(response)
case .failure(let error):
fatalError("\(error)")
}
}
}
而且我在不同的字段中得到此响应:
[Result]: success({
"incomplete_results" = 0;
items = (
{
"username" = " ... "
...
如何在Swift中获取某些特定字段,例如“用户名”?我想拥有所有用户的所有用户名并将其存储,您能帮我吗?
在https://jsonparseronline.com/中粘贴您的响应以查看对象的结构。它将显示您可以使用下标访问的所有键和值。
您需要提供一种类型以将JSON响应解析为。使用quicktype.io这样的网站可以从JSON生成一个网站,但是任何Decodable
类型都可以。然后,您可以使用Alamofire的responseDecodable
处理程序来解析您的响应。
AF.request(url).responseDecodable(of: TypeToParse.self) { response in
debugPrint(response)
}