如何使用swift从firebase中的JSON文件中获取电子邮件ID

时间:2017-02-16 17:02:13

标签: ios json swift firebase firebase-realtime-database

JSON Image我想查询:如果我发送电子邮件,那么我必须获取ID。目前我可以获取电子邮件或ID,但我需要以下示例的解决方案。

例如 1)如果我提供sandeep@gmail.com,那么我需要它的ID为123

如何在swift中实现这一目标?

这是我的JSON文件:

{
   "ID": 123,
   "Membership": 1234,
   "title": "Mr.",
   "firstname": "kumar",
   "lastname": "sandeep",
   "email": "sandeep@gmail.com",
   "membrshipstatus": "Active",
   "volunteer": "Yes",
   "creationDate": "2016-12-27 "
 },

{
   "ID": 452,
   "Membership": 4531,
   "title": "Mr.",
   "firstname": "kumar",
   "lastname": "sandeep",
   "email": "harry@gmail.com",
   "membrshipstatus": "Active",
   "volunteer": "Yes",
   "creationDate": "2016-11-17 "
 },

这是我的Swift代码:

    import UIKit
    import Firebase
    import FirebaseAuth

    class ViewController: UIViewController {

        @IBOutlet weak var emailofUser: UITextField!
        @IBOutlet weak var passwordofUser: UITextField!
        var ref:FIRDatabaseReference! //created a variable ref of type firebase database reference
        var databaseHandle:FIRDatabaseHandle? //to handle to database listener like to stop or start it


        var postdata = [String]?()
        var postall = [[String:String]]()


        override func viewDidLoad() {
            super.viewDidLoad()

            //set firebase reference
            ref = FIRDatabase.database().reference()

ref.child("1").child("email").observeEventType(.Value, withBlock: { snapshot in

            let emailid = snapshot.value as? String

            print(emailid)
        })

ref.child("1").child("ID").observeEventType(.Value, withBlock: { snapshot1 in

            let userID = snapshot1.value as? Int

            print(userID)
        })

            ref.queryOrderedByValue().observeEventType(.ChildAdded, withBlock: { snapshot2 in
                if let parentname = snapshot2.value as? Int {
                    print("The \(snapshot2.key) dinosaur's score is \(parentname)")
                }
            })
        }
    }

与此JSON图像相关的Swift代码:

override func viewDidLoad() {
        super.viewDidLoad()



            //set firebase reference
        ref = FIRDatabase.database().reference()

        let userRef = ref.child("1")

        //let queryRef = userRef.queryOrderedByChild("email").queryEqualToValue("mr.stefankirsch@gmx.com")


        userRef.queryOrderedByChild("email").queryEqualToValue("mr.stefankirsch@gmx.com").observeEventType(.Value, withBlock: { snapshot in

            for child in snapshot.children{

            let snap = child as! FIRDataSnapshot

                let userDict = snap.value as! [String:Any]

                let userId = userDict["ID"]
                let lastname = userDict["lastname"]
                print("\(userId!)  \(lastname!)")


            }
                    })

1 个答案:

答案 0 :(得分:0)

您应该使用用户的uid来将数据存储在用户节点中。假设您的用户节点被称为/ users ...就像这样

users
  uid_0
   "ID": 123,
   "Membership": 1234,
   "title": "Mr.",
   "firstname": "kumar",
   "lastname": "sandeep",
   "email": "sandeep@gmail.com"
  uid_1
   "ID": 452,
   "Membership": 4531,
   "title": "Mr.",
   "firstname": "kumar",
   "lastname": "sandeep"
   "email": "harry@gmail.com"

这是获取您询问的数据的查询

let usersRef = ref.child("users")
let queryRef = usersRef.queryOrdered(byChild: "email")
                       .queryEqual(toValue: "sandeep@gmail.com")
queryRef.observeSingleEvent(of: .value, with: { (snapshot) in

     for child in snapshot.children {
          let snap = child as! FIRDataSnapshot
          let userDict = snap.value as! [String: Any]
          let userId = userDict["ID"]
          let lastname = userDict["lastname"]
          print("\(userId!)  \(lastname!)")
      }
})

并打印结果:

123  sandeep