【问题标题】:JSON Serialization crashing in swiftJSON序列化迅速崩溃
【发布时间】:2014-06-12 15:06:24
【问题描述】:

我一直在使用以下代码行在 Objective-C 中解析 JSON 数据,但在 Swift 中同样的代码会导致应用程序崩溃。

NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:_webData
                          options:kNilOptions
                          error:&error];

我尝试使用NSJSONReadingOptions.MutableContainers,但似乎不起作用。我已经使用网上找到的各种 JSON 有效性检查器验证了从 Web 服务器获取的 JSON 数据的有效性。

[编辑] 我使用的 swift 代码如下:

let jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary

[更新]

使用let jsonResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary 可以解决问题。

【问题讨论】:

  • 您不能将 nil 与 NS_OPTIONS 样式的枚举一起使用 - NSJSONReadingOptions.fromMask(0) 将为您提供该选项类型的“以上都不是”值。
  • 记住+JSONObjectWithData:...的返回类型是id,而不是NSDictionary*。例如,该方法可以返回 NSArray*nil

标签: swift nsjsonserialization


【解决方案1】:

Xcode 给你的错误不是很有帮助,但看起来你需要以不同的方式声明你的 error 变量 (more at Apple's documentation),然后确保你处理字典返回的情况nil:

var error: AutoreleasingUnsafePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data,
        options:NSJSONReadingOptions.MutableContainers,
        error: error) as? NSDictionary
if jsonResult {
    // process jsonResult
} else {
    // couldn't load JSON, look at error
}

【讨论】:

  • 不。还是一样。
  • 你能再检查一下吗?那段代码对我产生了影响。您以这种方式将error 声明为指针,因此您不需要通过引用传递它(即error: error 而不是error: &amp;error)。
  • 我刚做了。它仍然是一样的。我注意到这个问题的一些奇怪之处在于它只对某些 JSON 输入有问题
  • 抱歉 - 我的原始代码出现编译器错误,所以我试图解决这个问题。我已经更新了您应该如何处理 JSON 序列化失败的情况。
  • 发生崩溃是因为您试图访问隐式展开的 Optional 的值(来自 NSJSONSerialization.JSONObjectWithData() 的返回值),而该值是 nil。该方法可能应该返回 AnyObject? 而不是 AnyObject! 以更清楚地说明您必须考虑这种情况。
【解决方案2】:

所有答案都不适合我。这有效(21.01.2015 - Xcode 6.1.1 / iOS 8.1.2):

 var err: AutoreleasingUnsafeMutablePointer<NSError?> = nil

 var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error:err) as NSDictionary

在这里找到它:Use of undeclared type AutoreleasingUnsafePointer Xcode 6 beta 6

【讨论】:

    【解决方案3】:

    Nil 必须工作,我认为您的错误来自另一个问题,请发布更多代码/崩溃日志

        var err: NSError
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options:    NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
    

    你可以试试吗?

    【讨论】:

    • 我一直在使用下面这行代码在 Objective-C 中解析 JSON 数据
    • 使用 0 会出现此错误Cannot convert the expression's type 'NSDictionary' to type 'NSJSONReadingOptions'
    • 你能发布你的 swift 代码吗?我刚刚阅读了文档,nil 必须工作。你最后有“as NSDictionary”吗?
    • 您的代码很好,也许崩溃不是来自您的 json 选项,崩溃在说什么?
    • 它显示了Thread 1: EXC_BREAKPOINT(code=EXC_I386_BPT,sub code=0x0"的线程
    【解决方案4】:
    // Created a NSDictionary to hold the data
    var tempLocations : NSArray = NSArray()
    var modelLocation : [Location] = [] // Will hold all the locations read in from datafile
    // setup the path for the data
    let jsonData = NSData(contentsOfURL: fullPathForDataFile)
    
    if let realJsonData = jsonData { // doing a test to check that the data exists
        tempLocations = NSJSONSerialization.JSONObjectWithData(realJsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSArray
        println("imported location data file with \(error) errors")
    } else {
        // The data file does not exist, tell the user!
    }
    
    // For each set of object data in the json file do a loop.
    for room in 1..<tempLocations.count {
        var newlocation : Location // This object contains var code:String?
        newlocation.code = tempLocations[room].valueForKey("code") as String
        // save the new location somewhere.
        modelLocation.addObject(newLocation)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多