Firebase和swiftyJSON解析
swiftyJSON非常适合解析Firebase。我们希望最终有两种可能性: 一个单独ID的Firebase和swiftyJSON解析,json,firebase,firebase-realtime-database,swifty-json,Json,Firebase,Firebase Realtime Database,Swifty Json,swiftyJSON非常适合解析Firebase。我们希望最终有两种可能性: 一个单独ID的imageName,以及所有ID的imageNames列表。JSON结构: 检索JSON的代码很好,但不显示imageName值: ref.observe(FIRDataEventType.value) { (snapshot: FIRDataSnapshot!) in let json = JSON(snapshot.value)
imageName
,以及所有ID的imageName
s列表。JSON结构:
检索JSON的代码很好,但不显示imageName
值:
ref.observe(FIRDataEventType.value) {
(snapshot: FIRDataSnapshot!) in
let json = JSON(snapshot.value)
// print("JSON: \(json)")
// PRINT IMAGENAME - NOT WORKING
if let imageName = json[12345][0]["imageName"].string {
print("imageName: \(imageName)")
}
// // PRINT IMAGENAME - NOT WORKING
let theKeys = json.dictionary!.keys
for key in theKeys {
let imageName = json[0][0]["imageName"]
print("imageName: \(imageName)")
}
}
我的最终结果是在CollectionView中显示一个imageURL。这似乎很接近,只是没有得到正确的快速JSON格式。谢谢。首先,我认为您应该使用“firebase方式”解析数据,我想您可以调用它,而不是使用SwiftJSON。这就是我用“火基方式”的方法
import FirebaseDatabase
class ViewController: UIViewController {
var ref: FIRDatabaseReference!
override fun viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
}
func retrieveData() {
ref.child("12345").observe(.childAdded, with { (snapshot) in
let dict = snapshot.value as? [String: AnyObject]
let imageNamed = dict!["imageName"] as? String
print("\(imageNamed)")
}
}
}
上面的代码对我很有效
在SwiftJSON中,我不能100%确定这是否有效,但也许我们可以试试
let json = JSON(snapshot.value) as? [String: AnyObject]
if let imageName = json!["12345"][0]["imageName"].string {
}
let theKeys = json.dictionary!.keys
for key in theKeys {
let imageName = json[0][0]["imageName"]
print("imageName: \(imageName)")
}