SwiftyJSON读取文件 - null

时间:2015-07-15 08:17:21

标签: ios json swift swifty-json

我正在尝试读取项目文件夹中的j.json文件。执行代码时没有出错,但是我得到的只是“null”而不是打印出来。初始文件扩展名为.rtf。

let jsonFilePath:NSString = NSBundle.mainBundle().pathForResource("j", ofType: "json")!
let jsonData:NSData = NSData(contentsOfFile: jsonFilePath as String, options: .DataReadingMappedIfSafe, error: nil)!
let json = JSON(data: jsonData)
println(json)

更新

问题是我使用了rtf文件(就像提到here)。这是正确的代码。

let jsonFilePath:NSString = NSBundle.mainBundle().pathForResource("<INPUT FILE NAME>", ofType: "<FILE EXTENTION>")!
let jsonData:NSData = NSData(contentsOfFile: jsonFilePath as String, options: .DataReadingMappedIfSafe, error: nil)!
var error:NSError?
let json = JSON(data: jsonData, options: .AllowFragments, error: &error)
if error != nil{
   println(error!.localizedDescription)
   }
   else{
       println(json)
       }

0 个答案:

没有答案