如何获取值json [英] How to get value json

查看:87
本文介绍了如何获取值json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用程序

可保存来自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/iOS编程/评论/acmuxu/parallax_table_view_header/.json

请帮助我阅读该值 预先感谢

解决方案

我已经从 HERE 是您JSON的链接. /p>

您可以通过以下方式从响应中获得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 项目以获取更多信息.

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

解决方案

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.

这篇关于如何获取值json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆