使用Swift从AFNetworking访问JSON数据

时间:2021-10-02 15:55:20

I have access a Yahoo Finance data set with the link in the code. It executes and prints the JSON data in the debug window. How would I store and then print selected fields in myNameLabel. For example the "symbol" or "LastTradePriceOnly" fields?

我可以使用代码中的链接访问Yahoo Finance数据集。它在调试窗口中执行并打印JSON数据。如何在myNameLabel中存储然后打印选定的字段。例如“symbol”或“LastTradePriceOnly”字段?

class ViewController: UIViewController {


    @IBOutlet weak var myNameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let manager = AFHTTPRequestOperationManager()

        manager.GET( "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=",
            parameters: nil,
            success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
                println("JSON: " + responseObject.description)

                //How to access and print individual fields?

            },
            failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
                println("Error: " + error.localizedDescription)
            })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1 个解决方案

#1


5  

You can just navigate the structure of nested dictionaries, perhaps something like:

您可以只导航嵌套字典的结构,可能是这样的:

manager.GET( "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=",
    parameters: nil,
    success: {
        operation, responseObject in

        if let quote = responseObject?.objectForKey("query")?.objectForKey("results")?.objectForKey("quote") as? NSDictionary {

            let symbol = quote.objectForKey("Symbol") as? String
            let lastTradePriceOnly = quote.objectForKey("LastTradePriceOnly") as? String

            println("results: \(symbol) @ \(lastTradePriceOnly)")
        } else {
            println("no quote")
        }
    },
    failure: {
        operation, error in

        println("Error: " + error.localizedDescription)
    })

#1


5  

You can just navigate the structure of nested dictionaries, perhaps something like:

您可以只导航嵌套字典的结构,可能是这样的:

manager.GET( "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=",
    parameters: nil,
    success: {
        operation, responseObject in

        if let quote = responseObject?.objectForKey("query")?.objectForKey("results")?.objectForKey("quote") as? NSDictionary {

            let symbol = quote.objectForKey("Symbol") as? String
            let lastTradePriceOnly = quote.objectForKey("LastTradePriceOnly") as? String

            println("results: \(symbol) @ \(lastTradePriceOnly)")
        } else {
            println("no quote")
        }
    },
    failure: {
        operation, error in

        println("Error: " + error.localizedDescription)
    })