Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase和swiftyJSON解析_Json_Firebase_Firebase Realtime Database_Swifty Json - Fatal编程技术网

Firebase和swiftyJSON解析

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)

swiftyJSON非常适合解析Firebase。我们希望最终有两种可能性:

一个单独ID的
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)")
}