简体   繁体   中英

How to grab nested data in a firebase realtime database?

I've made a small website for family and friends to guess after name, gender, date of birth, etc of our next baby. I have used vue in combination with firebase's real time database. Now I want to write a console app in c# to validate all the bets and point a winner. The problem is the way I set up the database:

-Users
    -Davy vettorato
        -posts
            -M5HuYYaUuKgqpD9qdVT
                date: "2020-09-23"
                gender: "Meisje"
                weight: "2550"
                length: "52"
                name: "Nele"
                extra_question: "11:00"
    -Dries
        -posts
            -MAerWe2LjQHGM6vEVur
                date: "21/9/2020"
                gender: "Meisje"
                weight: "4,200"
                length: "55"
                name: "Elise"
                extra_question: "11:45"
           -MAerhMgLbAe_sUyg5AE
                date: "21/9/2020"
                gender: "Jongen"
                weight: "4,200"
                length: "55"
                name: "Thibaut"
                extra_question: "11:46"

I'm able to get each user, but then I get stuck. I want to betting objects for each user derived from the db. This is what I've allready started with

        IFirebaseConfig config = new FirebaseConfig
        {
            AuthSecret = "//",
            BasePath = "https://babyproject-fb0.firebaseio.com/"
        };
            IFirebaseClient client = new FireSharp.FirebaseClient(config);

            if (client != null)
            {
                Console.WriteLine("Connected");
            }

            FirebaseResponse response = client.Get("");
            

            var json = response.Body;
            var result = JObject.Parse(json);

Does anybody knows how i can get all the seperate bets of each user? I've tried with selectTokens, without any succes..

 var json = response.Body;
        var result = JObject.Parse(json);

        foreach (var user in result.Children())
        {
            Better better = new Better();

            if (user is JProperty)
            {
                var prop = user as JProperty;
                better.Name = prop.Name.ToString();
                userBetList.Add(better);

                var userPosts = result[better.Name]["posts"].Children();

                foreach (var post in userPosts)
                {
                    var userPost = post as JProperty;
                    var betId = userPost.Name.ToString();
                    var betsOfId = result[better.Name]["posts"][betId].Children();
                    Bet generatedBet = new Bet();

                    foreach (var madeBet in betsOfId)
                    {
                        var bet = madeBet as JProperty;
                        if (bet.Name.ToString() == "datum")
                        {
                            generatedBet.Datum = DateTime.Parse(bet.Value.ToString());
                        }

This is the example of how I fixed the problem. I think there must be an easier way, but it did the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM