gpt4 book ai didi

ios - 无法从 JSON 中提取数组

转载 作者:可可西里 更新时间:2023-11-01 02:18:15 24 4
gpt4 key购买 nike

我正在尝试解析来 self 的服务器的 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 类型的键和输入任何”。

接下来,获取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)")
}

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


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

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

}

但这给了我:

WARNING: Cast from 'NSMutableArray' to unrelated type '[[String : AnyObject]]' always fails

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

ERROR: Call can throw, but it is not marked with 'try' and the error is not handled.

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

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”)是没有根据的,代码执行得很完美...

说真的,苹果?

最佳答案

正如您已经发现的,它返回 NSMutableDictionaryNSMutableArray。在 Swift 中将其转换为字典数组:

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

这就是为什么我更喜欢 SwiftyJSON 而不是 NSJSONSerializer 的原因。

你看到的 if var 可能比 if let 少很多。 if var 用于保持数组可变,因为您指定了 MutableContainers 选项。

关于ios - 无法从 JSON 中提取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34446670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com