I am trying to use swift to request an API and then parse the data and use it as I see fit. I have been able to request the API but I am confused about the process.
In Javascript the following steps are involved:
- Make a HTTP Request ( GET, POST etc )
- You get data back in JSON form
- Create a local variable and save the incoming JSON
- If the incoming JSON is an array then use a for loop to loop through it.
- If the JSON is a single object then simply use the "myObject.value" technique to extract data
Below is the code for my Swift HTTP request. I am getting the data back and it is being saved within "jsonResult" but I can seem to parse the data.
Can anyone explain what the are main steps involved in making HTTP requests and parsing the data in swift. And when to use NSArray and NSDictionary?
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground // add this in
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
let url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk"
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) { data, response, error in
// Handle result
println(NSString(data: data, encoding: NSUTF8StringEncoding))
//HOW TO PARSE THE DATA
}.resume()
The println is giving the following OutPut:
Any help will be greatly appreciated.