读取JSON文件
let filePath: NSString = NSBundle.mainBundle().pathForResource("obj", ofType: "json")!
let jsonUrl = NSURL(fileURLWithPath: filePath as String)
let jsonData = NSData(contentsOfURL: jsonUrl)
do{
let json = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions())
let lang:AnyObject = json.objectForKey("language")!
print(lang)
}catch{
print(error)
}
生成JSON数据
let dict = ["name":"jikexueyuan","age":1]
do{
let jsonData = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions())
let str = NSString(data: jsonData, encoding: NSUTF8StringEncoding)!
print(str)
}catch{
print(error)
}