Sudhanshu.B
Sudhanshu.B

Reputation: 636

how to read data from json in swift2

I am trying to read email from a json file in swift(2.2) which is:

 { "employees" : [
  {
    "name": "sudhanshu",
    "email": "[email protected]",
    "password": "password"
    "profilePic": ""
 },
 {
    "name": "prokriti",
    "email": "[email protected]",
    "password": "password@123",
    "profilePic": ""
  }
]}

But i am getting error " Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 128." UserInfo={NSDebugDescription=Unescaped control character around character 128.}" i have seen earlier posts but unable to find where exactly problem is??

if let path = NSBundle.mainBundle().pathForResource("Employees", ofType: "json") {
        if let data = NSData(contentsOfFile: path) {
            do {
                let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

                if let error = jsonResult["error"] {
                    print("Error is: \(error)")
                } else {
                    if let person = jsonResult["email"] {
                        print(person) // dictionary[@"quotables"]
                    }
                }

            } catch let error as NSError {
             print("Error is: \(error)")
            }

        }
    }

Thanks in advance!

Upvotes: 0

Views: 153

Answers (2)

Hemali Luhar
Hemali Luhar

Reputation: 359

You are trying to access directly email key from the dictionary. while you need to first access array from the key "employees" & then you need to get value from "email" key.

if let path = NSBundle.mainBundle().pathForResource("Employees", ofType: "json") {
if let data = NSData(contentsOfFile: path) {
    do {
        let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

        if let error = jsonResult["error"] {
            print("Error is: \(error)")
        } else {
            let person = jsonResult["employees"] as! NSArray
            for i in 0..<person.count
            {
                let dict = person.objectAtIndex(i) as! NSDictionary
                let strEmail = dict["email"] as! String
                print(strEmail)
            }
        }

    } catch let error as NSError {
        print("Error is: \(error)")
    }

}

Upvotes: 0

Eric Aya
Eric Aya

Reputation: 70098

"password": "password”

should be

"password": "password"

You have an invalid character instead of a ".

Update

Now that you've fixed your invalid character, you can access your data. But you're trying to cast as an NSDictionary something that's actually an array, if I believe the JSON excerpt you showed us.

So you should do something like this instead in your do:

if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String: String]] {
    for jsonDictionary in jsonResult {
        if let person = jsonDictionary["email"] {
            print(person)
        }
    }
}

Update and fix

if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] {
    if let employees = jsonResult["Employees"] as? [[String:String]] {
        for employee in employees {
            if let person = employee["email"] {
                print(person)
            }
        }
    }
}

Upvotes: 5

Related Questions