gpt4 book ai didi

ios - 无法在 swift 3 中获取 jsondata?

转载 作者:行者123 更新时间:2023-11-29 11:41:04 25 4
gpt4 key购买 nike

这里我需要检查键 img_type 的值,如果它是 1,那么我需要获取 img_file 的键值对,如果不是 2,那么我需要获取键值对 img_url 但我在检查值时卡在了这里我在使用这段代码时遇到错误任何人都可以帮助我如何实现这个吗?

{
"id": "1",
"name": "banner1",
"status": "1",
"display_from": null,
"display_to": null,
"img_type": "1",
"img_file": "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/5.jpg",
"img_url": "",
"img_title": "banner1",
"img_alt": "banner1",
"url": "",
"is_open_url_in_new_window": "1",
"is_add_nofollow_to_url": "1"
},
{
"id": "5",
"name": "banner5",
"status": "1",
"display_from": null,
"display_to": null,
"img_type": "2",
"img_file": "https://192.168.1.11/magento2/pub/media/ewl_bannerslider/slides/",
"img_url": "https://www.dtelepathy.com/blog/wp-content/uploads/2015/06/Technique-for-Faster-Web-Development.jpg",
"img_title": "",
"img_alt": "",
"url": "",
"is_open_url_in_new_window": "1",
"is_add_nofollow_to_url": "1"
}

func bannerDownloadJsonWithURL(){
let url = URL(string: bannerUrl)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil { print(error!); return }
do {
if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] {
for item in jsonObj {
if let itemDict = item as [String:Any]! {
for item in itemDict {
if item.key["img_type"] == 1 {

}
}
}
}
DispatchQueue.main.async {

}
}
} catch {
print(error)
}
}
task.resume()
}

最佳答案

你必须像下面这样使用:

if let itemDict = item as [String:Any]! {
if let imgTypeString = itemDict["img_type"] as? String, let imgType = Int(imgTypeString) {
if imgType == 1 {
print("img_file", itemDict["img_file"])
} else if imgType == 2 {
print("img_url", itemDict["img_url"])
}
} else {
print("Unable to parse img_type")
}
}

关于ios - 无法在 swift 3 中获取 jsondata?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46557821/

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