我正在尝试从swift(2.2)中的json文件读取电子邮件:
{ "employees" : [
{
"name": "sudhanshu",
"email": "[email protected]",
"password": "password"
"profilePic": ""
},
{
"name": "prokriti",
"email": "[email protected]",
"password": "password@123",
"profilePic": ""
}
]}
但是我收到错误消息“ Error Domain = NSCocoaErrorDomain Code = 3840”字符128附近的转义控制字符。“ UserInfo = {NSDebugDescription =字符128附近的转义控制字符。}”我见过较早的帖子,但找不到确切的问题出在哪里? ?
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)")
}
}
}
提前致谢!
您正在尝试直接从词典中访问电子邮件密钥。而您首先需要从“员工”键访问数组,然后需要从“电子邮件”键获取价值。
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)")
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句