ios - 使用 Alamofire 从 Firebase 检索 JSON

标签 ios json swift firebase alamofire

这是我的代码:

let urlWithId = url + buildingInfo.building.identifier + ".json"
Alamofire.request(urlWithId).responseJSON{ (response) in
    print(response.result.value)
}

当打印 Any? 类型的值时,我得到这个:

Optional(<__NSArrayI 0x123456789012>(
<null>,
{
    aaa = 111;
    bbb = "bbb";
    ccc = 000;
    ddd = "ddd";
    eee =     (
        a,
        b,
        c
    );
}
)
)

这应该有效,但总是返回 nil:

if let value = response.result.value as? NSDictionary {
    // Never gets into here, always nil
}

我想获取一个 JSON:

  • aaa: unsigned long
  • bbb: 字符串
  • ccc:无符号长整型
  • ddd: 字符串
  • eee: json

如果我在 Safari 中执行请求,我会得到:

[
    null,
    {
        "aaa":111,
        "bbb":"bbb",
        "ccc":000,
        "ddd":"ddd",
        "eee":[
                  "a",
                  "b",
                  "c"
              ]
    }
]

有什么想法吗?也许另一种使用 Firebase URL 请求 json 的方式

最佳答案

正如我看到的 JSON 响应,您获取的不是字典,而是数组。您需要的响应位于索引 1 中。所以解析为:

In your response JSON array:

index 0 = null

index 1 = array

将您的代码更新为:

if let value = response.result.value as? [Any], let dict = value[1] as? [String: Any] {
    print(dict)
}

关于ios - 使用 Alamofire 从 Firebase 检索 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885753/

相关文章:

ios - 如何在 UIStackView 中无间隙地定位旋转的 UILabel?

ios - 如何设置UICollectionViewCell大小

java - 将 JSON 响应映射到相应的 POJO

objective-c - iOS App 中是否创建了很多自动释放池?

json - spring-mvc 返回原始 json 字符串

javascript - 使用 Javascript 解析 JSON 并使用 HTML/CSS 设置样式

ios - SwiftUI:如果在 ForEach 中使用,NavigationLink 会立即弹出

ios - 由于启动时崩溃,应用程序被拒绝两次

iphone - UITableViewCell 去除每行之间的白色并显示 UIView 的背景图像

ios - 对象加载 UIImage 使用 swift 使程序执行崩溃