无法从 JSON 中提取数组
Can't Extract Array from JSON
我正在尝试解析来自我的服务器的 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
的值似乎是一个符合 "an array of dictionaries, each with keys of type String
and values of type 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]] {
}
但这给了我:
WARNING: Cast from 'NSMutableArray' to unrelated type '[[String :
AnyObject]]' always fails
在if
块的位置,并且(等待它):
ERROR: Call can throw, but it is not marked with 'try' and the error
is not handled.
...在 AppDelegate.swift(CoreData 样板),完全相同的症状 。
CoreData 代码一直在编译。 如果我注释掉 if var nativeItems = items...
转换,错误就会消失。更改变量名称无效。确实。巫毒
幸运的是,我可以通过扩展样板 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {...
部分中的 catch
子句来绕过它:
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
}()
...令人惊讶的是,上面的警告 ("cast always fails") 是没有根据的,代码执行得很完美...
说真的,苹果?
正如您已经发现的那样,它 returns 是 NSMutableDictionary
的 NSMutableArray
。将其转换为 Swift 中的字典数组:
if var items = items as? [[String: AnyObject]] {
print(items[0])
} else {
print("Really?")
}
这就是为什么我更喜欢 SwiftyJSON 而不是 NSJSONSerializer
的原因。
您看到的 if var
可能比 if let
少很多。 if var
用于保持数组可变,因为您指定了 MutableContainers
选项。
let itemsArray:NSMutableArray = NSMutableArray()
itemsArray = dictionary["items"]
print(itemsArray)
这样试试..
我正在尝试解析来自我的服务器的 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
的值似乎是一个符合 "an array of dictionaries, each with keys of type String
and values of type 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]] {
}
但这给了我:
WARNING: Cast from 'NSMutableArray' to unrelated type '[[String : AnyObject]]' always fails
在if
块的位置,并且(等待它):
ERROR: Call can throw, but it is not marked with 'try' and the error is not handled.
...在 AppDelegate.swift(CoreData 样板),完全相同的症状
CoreData 代码一直在编译。 如果我注释掉 if var nativeItems = items...
转换,错误就会消失。更改变量名称无效。确实。巫毒
幸运的是,我可以通过扩展样板 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {...
部分中的 catch
子句来绕过它:
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
}()
...令人惊讶的是,上面的警告 ("cast always fails") 是没有根据的,代码执行得很完美...
说真的,苹果?
正如您已经发现的那样,它 returns 是 NSMutableDictionary
的 NSMutableArray
。将其转换为 Swift 中的字典数组:
if var items = items as? [[String: AnyObject]] {
print(items[0])
} else {
print("Really?")
}
这就是为什么我更喜欢 SwiftyJSON 而不是 NSJSONSerializer
的原因。
您看到的 if var
可能比 if let
少很多。 if var
用于保持数组可变,因为您指定了 MutableContainers
选项。
let itemsArray:NSMutableArray = NSMutableArray()
itemsArray = dictionary["items"]
print(itemsArray)
这样试试..