lresner / pc-ios-essentials-swiftyJSON

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using NYC Open Data Using SwiftyJSON

json

We've said many times before, the internet is essentially a huge database. Your Facebook photos, the weather history for El Paso, Texas, the headlines for the International Herald Tribune, all of the gyro shops in Istanbul. Some of those sites, or some organizations, make this data available for developers to build cool things with.

  • Much of the time, these data are returned to you in a format called JSON.
  • We are going to utilize JSONs from NYC Open Data for our final projects.
  • JSON probably looks intimidating, but it's essentially a nested hash. Let's call this hash object response.

Using JSON with Swift. Under normal circumstances, "parsing" a JSON, or searching it for the information that you want, is an ugly process. Enter a library called SwiftyJSON. SwiftyJSON is a library that is easy to implement that makes it easier for us to parse JSON.

Implementing Swifty JSON

  • Go to this site.
  • Create a new Swift file in your project, name is SwiftyJSON.swift and paste the SwiftyJSON.swift contents there.
  • In your ViewDidLoad method in your view controller, paste the following code:
let filePath = NSBundle.mainBundle().pathForResource("rows", ofType: "json")
        let data =  NSData(contentsOfFile: filePath!)
        //print(data)
        let json = JSON(data: data!)
        var actualData = json["data"]
        
        

In this case rows refers to the filename of your JSON.

And you're good to go!

View this lesson on Learn.co

About