26_Json_Example
JSON 很通用的处理数据的工具,各个语言都可以使用。
这个App就是把一个网上的用JSON格式保存的数据拿下来,然后保存到字典中,显示出来,用iOS自己的方法。
一定要记得写最后的那个 task.resume() ,很多问题就出在这里。可以每次先把它写上!
// // ViewController.swift // Json Example // // Created by zcdll on 16/1/23. // Copyright © 2016年 ZC. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let url = NSURL(string: "https://freegeoip.net/json/")! let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in if let urlContent = data { //let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding) //print(urlContent) do { let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers) print(jsonResult["country_name"]!) } catch { print("JSON serialization failed") } } else { } } task.resume() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }