Parsing JSON

keyX, valueX: numbers, array, even dictionary

Parsing JSON
1. get the raw JSON data
2. parse the JSON data into a funcation object
3. grab the data from the foundation object

if let photosDictionary =
    parseResult[Constants.FlickrResponseKeys.Photos] as? [String:
    AnyObject],
    photoArray = photosDictonary[Constants.FlickrResponseKeys.Photo] as?
        [String:AnyObject]{

         let randomPhotoIndex = Int(ar:4random_uniform(UInt32(photoArray.count)))
        }
private func getImageFromFlickr(){
    
    let methodParameters = [
        Constants.FlickrParameterKeys.Method: Constants.FlickrParameterValues.GalleryPhotosMethod,
        Constants.FlickrParameterKeys.APIKey: Constants.FlickrParameterValues.APIKey,
        Constants.FlickrParameterKeys.GalleryID: Constants.FlickrParameterValues.GalleryID,
        Constants.FlickrParameterKeys.Extras: Constants.FlickrParameterValues.MediumURL,
        Constants.FlickrParameterKeys.Format: Constants.FlickrParameterValues.ResponseFormat,
        Constants.FlickrParameterKeys.NoJSONCallback: Constants.FlickrParameterValues.DisableJSONCallback   
    ]

    let session = URLSession.shared
    lel urlString = Constants.Flickr.APIBaseURL + escapedParameters(methodParameters as [String:AnyObject]) 
    let url = URL(string: urlString)!
    let request = URLRequest(url: url)

    let task = session.dataTask(with: request){ (data, response, error) in

        func displayError(_ error: String){
            print(error)
            print("URL at time of error: \(url)")
            performUIUpdatesOnMain{
                self.setUIEnabled(true)
            }

        guard(error == nil) else {
            displayError("There was an error with your request: \(error)")
            return
        }

        guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode
            displayError("Your request returned a status code other than 2xx!") 
            return       
        }

        guard let data = data else {
            displayError("No data was returned by the request!")
            return
        }

        let parsedResult: [String:AnyObject]!
        do {
            parsedResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:AnyObject])
        } catch {
            displayError("Could not parse the data as JSON:'\(data)'")
            return
        }

        guard let stat = parsedResult[Constants.FlickrResponseKeys.Status] as ? String, stat == Constants.FlickrResponseValues.OKStatus else {
            displayError("Cannot find keys '\(Constants.FlickrResponseKeys.Photos)'")
            return        
    }  

    let randomPhotoIndex = Int(arc4random_uniform(UInt32(photoArray.count)))
    let photoDictionary = photoArray[randomPhotoIndex] as [String:AnyObject]
    let photoIitle = photoDictionary[Constants.FlickrResponseKeys.Title]as? String
   
    guard let imageUrlString = photoDictionary[Constants.FlickrResponseKeys.MediumURL] as? String else {
            displayError("Cannot find key '\(Constants.FlickrResponseKeys.MediumURL)' in \(photoDictionary)")
            return
}

    let imageURL = URL(string: imageUrlString)
    if let imageData = try? Data(contentsOf: imageURL!){
        performUIUpdatesOnMain {
            self.setUIEnabled(true)
            self.photoImageView.image = UIImage(data: imageDAta)
            self.photoTitleLabel.text = photoTitle ?? "(Untitled)"
        }
    } else {
        displayError("Image does not exist at \(imageURL)")
    }

    task.resume()