firebase数组解析json总是失败

时间:2017-04-20 09:12:43

标签: json swift firebase firebase-realtime-database

这是我打印的print(childSnap)并尝试使用as? [String : Any]进行解析,但总是失败。

   Snap (-Ki9GkBvIMofJiIByVeI) {
        location = Cupertino;
        name = "find hotel ";
        userCreatedId = "";
    }
 let ref = FIRDatabase.database().reference()
        let allGroup = ref.child("all-group")
        allGroup.observe(.childAdded, with: { (snapshot) in
            for  childSnap in snapshot.children{       
                   print(childSnap)

                if let _childSnap = childSnap as? [String : Any] {
                    //this code not execute.. 
                    let group = Group(json: _childSnap )
                    groups.append(group)
                }
            }

        }, withCancel: nil)
    }

1 个答案:

答案 0 :(得分:1)

将广告childSnap输入到FIRDataSnapshot,然后访问它的value属性。

if let _childSnap = (childSnap as! FIRDataSnapshot).value as? [String : Any] {
    let group = Group(json: _childSnap )
    groups.append(group)
}