这是我的代码:
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/