gpt4 book ai didi

ios - 如何通过 Alamofire 和 SwiftyJSON 在 UIViewController 中使用根目录中的 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 12:08:36 25 4
gpt4 key购买 nike

我想使用 Alamofire 和 SwiftyJSON 解析 JSON。

要解析的 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"
}

我可以访问“args”,但无法访问 UIViewControllerUILabel 中的“Accept-Encoding”

我在编码中尝试的内容:

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]]
}
}

我想在我的 UIViewControllerUILabel 中使用“标题”。

最佳答案

您正在尝试将 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
}
}

关于ios - 如何通过 Alamofire 和 SwiftyJSON 在 UIViewController 中使用根目录中的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com