Reputation: 43
I am trying to create an application
that saves video from 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
Please help me to read the value Thanks in advance
Upvotes: 2
Views: 83
Reputation: 71854
I have created Codable
from the JSON
you provided from HERE And HERE is the link with your JSON
.
And you can get fallback_url
below way from response:
@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()
}
Check Out DEMO project for more info.
Upvotes: 2