我正在尝试创建一个应用程序
从Reddit保存视频
import UIKit
struct Country:Decodable {
let fallback_url: String
}
class ViewController: UIViewController {
@IBOutlet weak var textUrl: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnDownload(_ sender: Any) {
let linkk = (textUrl.text!)
let url = "\(linkk).json"
let urlObj = URL(string: url)
URLSession.shared.dataTask(with: urlObj!) { (data, response, error) in
do {
let countries = try JSONDecoder().decode([Country].self, from: data!)
for country in countries {
print(country.fallback_url)
}
} catch {
print("Error")
}
}.resume()
}
}
https://www.reddit.com/r/iOSProgramming/comments/acmuxu/parallax_table_view_header/.json
请帮助我阅读该值 预先感谢
答案 0 :(得分:2)
我已经根据您从HERE提供的Codable
创建了JSON
,并且HERE是与您的JSON
的链接。
您可以通过以下方式获得fallback_url
:
@IBAction func btnDownload(_ sender: Any) {
let url = "https://www.reddit.com/r/iOSProgramming/comments/acmuxu/parallax_table_view_header/.json"
let urlObj = URL(string: url)
URLSession.shared.dataTask(with: urlObj!) { (data, response, error) in
do {
let countries = try JSONDecoder().decode(Transactiondetails.self, from: data!)
for country in countries {
let childrens = country.data.children
for child in childrens {
if let secureMedia = child.data.secureMedia {
let fallback_url = secureMedia.redditVideo.fallbackURL
print(fallback_url)
}
}
}
} catch {
print("Error")
}
}.resume()
}
检出DEMO项目以获取更多信息。