使用Swift从AFNetworking访问JSON数据

使用Swift从AFNetworking访问JSON数据,第1张

概述我通过代码中的链接访问了Yahoo Finance数据集.它在调试窗口中执行并打印 JSON数据.如何在myNameLabel中存储然后打印选定的字段.例如“symbol”或“LastTradePriceOnly”字段? class ViewController: UIViewController { @IBOutlet weak var myNameLabel: UILabel! 我通过代码中的链接访问了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.    }}
解决方法 您可以只导航嵌套字典的结构,可能是这样的:

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=",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)    })
总结

以上是内存溢出为你收集整理的使用Swift从AFNetworking访问JSON数据全部内容,希望文章能够帮你解决使用Swift从AFNetworking访问JSON数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存