Unity c#迭代Firebase快照

时间:2017-12-04 15:40:21

标签: c# firebase unity3d firebase-realtime-database

我有这个json数据库

{ 
"shops" : {
        "7" : {
          "cod1" : {
            "name" : "name3",
            "description" : "xxx",

          },
          "cod2" : {
            "name" : "name2",
            "description" : "xxx",

          },
          "cod3" : {
            "name" : "name1",
            "description" : "xxx",

          },

我需要迭代快照以获取cod1,cod2,cod3,我需要为我之前不知道的那些代码做一些事情

public void load(string city){
//city is number 7 in this case;
        Firebase.Auth.FirebaseUser user = auth.CurrentUser;
        FirebaseDatabase.DefaultInstance
                        .RootReference.Child ("shops").Child (city)
            .GetValueAsync ().ContinueWith (task => {
            if (task.IsFaulted) {
                // Handle the error...
            } else if (task.IsCompleted) {
                DataSnapshot snapshot = task.Result;
                int i = 0;

                foreach (DataSnapshot shop in snapshot.Children) {
                        debug.text = "\nstarting with #" + i.ToString () + ": " + shop.Key+";\n";

                        i++;
                }

            }
        });

我需要debug.text来显示"从#1开始:cod1"然后再使用cod2和cod3

现在我得到类似的东西#34;从#1:7开始#34;这是那些代码的父母的关键

有没有办法获取这些代码?

1 个答案:

答案 0 :(得分:0)

嗯,首先,你的JSON并没有真正按照它应该的格式化。如果您想要准确地返回数据," shop"应该是一个数组,而不是一个对象。像这样:

"shops" : [
  "cod1" : {
        "name" : "name3",
        "description" : "xxx"
      },
      "cod2" : {
        "name" : "name2",
        "description" : "xxx"
      },
      "cod3" : {
        "name" : "name1",
        "description" : "xxx"
      }
]

目前,"商店"是一个对象,它有一个子对象" 7",它是cod1,cod2和cod3的父对象。这也是你看到" 7"的原因。