无法从JSON

时间:2015-12-24 02:53:46

标签: ios json swift nsjsonserialization

我正在尝试从我的服务器解析JSON格式的响应。

这是我的代码的第一部分:

// (responseBody is of type NSData)
guard let dictionary = try? NSJSONSerialization.JSONObjectWithData(responseBody!, options: [.MutableContainers]) else{
    fatalError("JSON Root Dictionary Not Found")
}

print("Dictionary: \(dictionary)")

...并且日志正确地给出了:

Dictionary: {
    count = 1;
    date = "2015-12-22T13:16:17.727";
    items =     (
                {
            attribute1 = null;
            attribute2 = null;
            attribute3 = null;
            date = "2015-12-22T17:30:52.764";
            size = 9175;
            version = 19;
        }
    );
}

(省略了相关字段,相关字段名称和值因机密性而更改)

...所以键items的值似乎是一个符合“字典数组的对象,每个字典都包含String类型的键和Any类型的值”。

接下来,获取items数组:

guard let count = dictionary["count"] as? Int else {
    fatalError("Item Count Not Found")
}

guard count > 0 else {
    fatalError("Item Count Is Zero")
}

guard let items = dictionary["items"]! else{
    fatalError("Items Array Not Found")
}

if items is Array<Any> { 
    // THIS TEST FAILS (SHOULD PASS?)
    print("Items is an array")
}
else if items is Dictionary<String, Any> {
    // THIS TEST FAILS, TOO (JUST OUT OF DESPERATION...)
    print("Items is a dictionary")
}
else{
    // ...SO THIS CODE RUNS.
    print("Really? \(items)")
}

但是 - 上面代码中的注释解释了 - 我无法将其转换为数组,而是执行了最后一次print()调用(print("Really? \(items)")),给出了:

Really? (
        {
        attribute1 = null;
        attribute2 = null;
        attribute3 = null;
        date = "2015-12-22T17:30:52.764";
        size = 9175;
        version = 19;
    }
)

...所以, items的类型是什么?如何获取我的数组?

也许我错过了一些关于Swift集合类型的东西?

注意:起初我怀疑数组元素是用方括号括起来的(())而不是方括号([])。但是,字典和数组的控制台输出似乎遵循这种格式,如the answers to this question中所述。

更新:根据以下评论中@ Paulw11提供的提示,我使用此代码解决了这个问题:

guard let items = dictionary["items"]! as? NSMutableArray else{
    fatalError("Items Array Not Found")
}

for element in documents {               // I wish I could combine these                 
    let item = element as! NSDictionary  // two lines into one (enumeration and cast)

    print("Item: \(item)")
}

...但是,我仍然不清楚如何实现不依赖于Foundation类的纯Swift解决方案。

更新2:我正在尝试使用以下代码将NSMutableArray强制转换为原生Swift数组:

if var nativeItems = items as? [[String: AnyObject]] {

}

但是这给了我:

  

警告:从'NSMutableArray'转换为无关类型'[[String:   AnyObject]]'总是失败

if块的位置,并(等待它):

  

错误:调用可以抛出,但没有标记为'try'和错误   没有处理。

...在AppDelegate.swift(CoreData样板文件)中,症状完全相同described here

CoreData代码一直在编译。如果我注释掉if var nativeItems = items...强制转换,则错误消失。更改变量名称无效。当然。巫毒

幸运的是,我可以通过扩展样板catch部分中的lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {...子句来解决这个问题:

catch let error as NSError {
        // Report any error we got.

        print("Error: \(error)")

        var dict = [String: AnyObject]()

        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"

        dict[NSLocalizedFailureReasonErrorKey] = failureReason

        dict[NSUnderlyingErrorKey] = error as NSError

        let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)

        // TODO: Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and 
        // terminate. You should not use this function in a shipping 
        // application, although it may be useful during development.

        NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")

        abort()
    }

    return coordinator
}() 

到此:

catch let error as NSError {
        // Report any error we got.

        print("Error: \(error)")

        var dict = [String: AnyObject]()

        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"

        dict[NSLocalizedFailureReasonErrorKey] = failureReason

        dict[NSUnderlyingErrorKey] = error as NSError

        let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)

        // TODO: Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and 
        // terminate. You should not use this function in a shipping 
        // application, although it may be useful during development.

        NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")

        abort()
    }
    catch {
        // << ADDED THIS "CATCH-ALL" >>
        fatalError()
    }

    return coordinator
}() 

...而且令人惊讶的是,上面的警告(“施法总是失败”)是没有根据的,代码执行完美......

严重,APPLE?

2 个答案:

答案 0 :(得分:1)

正如您已经发现的那样,它会返回NSMutableArray NSMutableDictionary。要将它转换为Swift中的字典数组:

if var items = items as? [[String: AnyObject]] {
    print(items[0])
} else {
    print("Really?")
}

这就是我更喜欢SwiftyJSON到NSJSONSerializer的原因。

您可能会发现if varif let少得多。此处使用if var来保持您的数组可变,因为您指定了MutableContainers选项。

答案 1 :(得分:0)

let itemsArray:NSMutableArray = NSMutableArray()

itemsArray = dictionary["items"]

print(itemsArray)

试试这样..