[英]How to use JSON objects from root in UIViewController with Alamofire and SwiftyJSON
I want to parse a JSON with Alamofire and SwiftyJSON. 我想用Alamofire和SwiftyJSON解析JSON。
JSON to parse: JSON解析:
{
"args": {},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-GB,en-US;q=0.8,en;q=0.6",
"Connection": "close",
"Cookie": "_gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
},
"origin": "150.107.254.75",
"url": "http://httpbin.org/get"
}
I can access the 'args' but I can't access the 'Accept-Encoding' in my UIViewController
's UILabel
我可以访问'args',但无法访问
UIViewController
的UILabel
的'Accept-Encoding'
What I am trying in coding: 我在尝试编码:
let Endpoint: String = "http://httpbin.org/get"
Alamofire.request(Endpoint).responseJSON { response in
if let json = response.result.value as? Array<Dictionary<String,Any>> {
self.arrRes = json as [[String:AnyObject]]
}
}
I want to use 'Headers' in my UIViewController
's UILabel
. 我想在
UIViewController
的UILabel
使用“ Headers”。
You're trying to cast the json to an array, but it's a dictionary, try this: 您正在尝试将json转换为数组,但这是一个字典,请尝试以下操作:
let endpoint: String = "http://httpbin.org/get"
Alamofire.request(endpoint).responseJSON { response in
if let json = response.result.value as? [String:Any],
let headers = json["headers"] as? [String:Any] {
self.label.text = headers["Accept-Encoding"] as? String
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.