saud alshaman
saud alshaman

Reputation: 43

How to get value json

I am trying to create an application

that saves video from Reddit

This value

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

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

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

Related Questions