Search code examples
jsonswiftnsdata

Converting JSON NSData to a dictionary


I have a JSON file like this.

{   
"Team Name" : "Team",
"Images" : "images",
"Players" : [
    {
        "Number" : "61",
        "Height" : "6'5\"",
        "Position" : "P",
        "DOB" : "Feb 22, 1989",
        "First Name" : "Chris",
        "Weight" : "210",
        "Last Name" : "Shmoe",
        "Bats" : "R",
        "Throws" : "R"
    },
    {
        "Number" : "54",
        "Height" : "6'3\"",
        "Position" : "P",
        "DOB" : "Dec 31, 1982",
        "First Name" : "Ronald",
        "Weight" : "240",
        "Last Name" : "Peter",
        "Bats" : "R",
        "Throws" : "R"
    },
    {
        "Number" : "67",
        "Height" : "6'4\"",
        "Position" : "P",
        "DOB" : "Sep 24, 1984",
        "First Name" : "Scott",
        "Weight" : "215",
        "Last Name" : "Tim",
        "Bats" : "R",
        "Throws" : "R"
    },

Heres What I have

  var downloadFailed = false
    var error: NSError? = nil

    let navigationController = self.window!.rootViewController as! UINavigationController
    let masterController = navigationController.childViewControllers[0] as! PlayerViewController

    if let url = NSURL(string: " ") {

        if let urlData = NSData(contentsOfURL: url) {
            let jsonString = NSString(data: urlData, encoding: NSUTF8StringEncoding)
            println(jsonString)
            var result = NSJSONSerialization.JSONObjectWithData(urlData, options: NSJSONReadingOptions.allZeros, error: nil) as? NSDictionary

            print("dictionary created")
            if let players = result?.objectForKey("Players") as? [[String:AnyObject]]
            {

                for dictionary in players{

                    let fname = players["First Name"]
                    let lname = array["Players"]?["Last Name": String]
                    let number = array["Players"]?["Number"]
                    let position = array["Players"]?["Position"]
                    let bats = array["Players"]?["Bats"]?!.string
                    let throws = array["Players"]?["Throws"]?!.string
                    let height = array["Players"]?["Height"]?!.string
                    let weight = array["Players"]?["Weight"]?!.string
                    let DOB = array["Players"]?["DOB"]?!.string


                   masterController.objects.append(Players(fName: (fname as String?)!, lname: (lname as String?)!, position: (position as String?)!, bats: (bats as String?)!, throws: (throws as String?)!, height: (height as String?)!, weight: (weight as String?)!, DOB: (DOB as String?)!, number: (number as String?)!))}

Currently the only thing I think I dont have correct is I dont know how to make the code read in properly so that it uses the key "Players" and reads the correct information from the JSON file.

EDIT:

I have been working on it a bunch and have gotten this now I have very few errors just no keeps crashing at

 if let dict = array as? [String: AnyObject]

But heres my new code for my load data function

 func preloadData() {


        var downloadFailed = false
        var error: NSError? = nil

        let navigationController = self.window!.rootViewController as! UINavigationController
        let masterController = navigationController.childViewControllers[0] as! PlayerViewController

        if let url = NSURL(string: "removed for reasons") {

            if let urlData = NSData(contentsOfURL: url) {

                if let array: AnyObject! = NSJSONSerialization.JSONObjectWithData(urlData, options: nil, error: &error)
                {
                    print (array)
                    if let dict = array as? [String: AnyObject]
                    {
                    if let players = dict["Players"] as? [AnyObject]
                    {
                    for dictionary in players {
                    let fname = dictionary["First Name"]
                    let lname = dictionary["Last Name"]
                    let number = dictionary["Number"]
                    let position = dictionary["Position"]
                    let bats = dictionary["Bats"]
                    let throws = dictionary["Throws"]
                    let height = dictionary["Height"]
                    let weight = dictionary["Weight"]
                    let DOB = dictionary["DOB"]


                   masterController.objects.append(Players(fName: fname as! String, lname: lname as! String, position: position as! String, bats: bats as! String, throws: throws as! String, height: height as! String, weight: weight as! String, DOB: DOB as! String, number: number as! String))
                        }
                        }
                }

                //masterController.objects.sort({$0.courseNumber < $1.courseNumber})
            }
            else
            {
                downloadFailed = true
            }
        } else {
            downloadFailed = true
        }
    } else {
        downloadFailed = true
    }

    if downloadFailed {
        let alert = UIAlertView(title: "Error", message: "Unable to download course data", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
    }
}

Solution

  • I realise that the question is using Swift 2, but since there are no answers and everyone should be using Swift 3, here is the Swift 3 solution.

    func preloadData(){
    
        func failed(msg: String){
    
            let alert = UIAlertController(title: "Error", message: msg, preferredStyle: .alert)
    
            alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
    
            self.addChildViewController(alert)
        }
    
        guard let navigationController = self.window!.rootViewController as? UINavigationController else { failed(msg: "The root view controller is not a UINavigationController"); return }
    
        guard let masterController = navigationController.childViewControllers[0] as? PlayerViewController else { failed(msg: "There is no PlayerViewController"); return }
    
        guard let url = URL(string: "Whatever your URL is") else { failed(msg: "This is an invalid URL"); return }
    
        guard let contents = try? Data(contentsOf: url) else { failed(msg: "Cannot retrieve data from this URL"); return }
    
        guard let json = try? JSONSerialization.jsonObject(with: contents, options: .mutableContainers) else { failed(msg: "Contents is not in JSON format."); return }
    
        guard let dict = json as? Dictionary<String,Any> else { failed(msg: "Cannot convert JSON to dictionary."); return }
    
        guard let players = dict["Players"] as? Array<Dictionary<String,String>> else { failed(msg: "There is no players."); return }
    
        for player in players {
    
            guard
                let firstName   = player["First Name"],
                let lastName    = player["Last Name"],
                let position    = player["Position"],
                let bats        = player["Bats"],
                let _throws     = player["Throws"],
                let height      = player["Height"],
                let weight      = player["Weight"],
                let dob         = player["DOB"],
                let number      = player["Number"] else { continue }
    
            masterController.objects.append(Player(firstName: firstName, lastName: lastName, position: position, bats: bats, throws: _throws, height: height, weight: weight, dob: dob, number: number))
    
        }
    }