urlからdataを取得する辺りから書くと下記のような感じ
class ViewController: UIViewController,NSXMLParserDelegate {
@IBOutlet weak var jsonTextView: UITextView!
@IBAction func Button_GetJson(sender: AnyObject) {
// let url:NSURL = NSURL(string: "http://editors.ascii.jp/c-minamoto/swift/swift-5-data.xml")!
// let request = NSURLRequest(URL: url)
//make URL of google feed api
let urlString = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://rss.itmedia.co.jp/rss/2.0/news_bursts.xml&num=8"
let url = NSURL(string: urlString)
// var titles:String = ""
//download by NSSession
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler:{data, response, error in
//convert json data to dictionary
var dict:NSDictionary
do {
dict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
//get responseData, feed, entries
let responseData = dict["responseData"] as! NSDictionary
let feed = responseData["feed"] as! NSDictionary
let entries = feed["entries"] as! NSArray
//extract entries
// print(entries.count)
var titles:String = ""
for entry in entries {
// print(entry["title"])
let title:String = entry["title"] as! String
titles += title + ":"
}
// print(titles)
dispatch_async(dispatch_get_main_queue()){
self.jsonTextView.text = titles
}
} catch {
print("error")
return
}
// print(dict)
})
task.resume()
// self.jsonTextView.text = titles
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
0 件のコメント:
コメントを投稿