1

我想从我的 Firebase json 文件中获取值目前我可以毫无问题地添加值但我无法读取它

我已经尝试了多种方法来循环它,但我总是卡住

这是我的 json

{  
   "23ds1f5":{  
      "email":"email@example.com",
      "name":"my name",
      "points":0
   },
   "dsfv57s":{  
      "email":"email2@example.com",
      "name":"this name",
      "points":0
   },
   "r6s7gv98268":{  
      "email":"lol@example2.com",
      "name":"nameHere",
      "points":0
   }
}

字段“23ds1f5”“dsfv57s”“r6s7gv98268”是自动生成的,我想获取电子邮件名称和点值这可能吗?

这是我现在的代码

FirebaseResponse response = await client.GetAsync("testing");
Data data = response.ResultAs<Data>();

            var json = response.Body;
            var firebaseLookup = JsonConvert.DeserializeObject<Data>(json);

            Console.WriteLine(json.ToString());


            var jsonObject = JObject.Parse(json);
            foreach (var tmp in jsonObject) {
                Console.WriteLine(tmp.Key);
            }

我不知道如何获得我尝试过的价值:

var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
            foreach (var kv in dict) {
                Console.WriteLine(kv.Key + ":" + kv.Value);
            }

但没有成功

我想email从每个人那里得到

更新:现在我可以使用此代码获取我的第一个元素,但我仍然无法获得价值

var jsonObject = JObject.Parse(json);
            foreach (var tmp in jsonObject) {
                Console.WriteLine(tmp);

            }

安慰:

[23ds1f5, {
  "email": "email@example.com",
  "name": "my name",
  "points": 0
}]

问题解决了!!

            Dictionary<string, Data> elist = JsonConvert.DeserializeObject<Dictionary<string, Data>>(json);

            foreach (KeyValuePair<string, Data> entry in elist) {
                // do something with entry.Value or entry.Key
                //Console.WriteLine(entry.Key);
                Console.WriteLine(entry.Value.name);
                Console.WriteLine(entry.Value.email);
                Console.WriteLine(entry.Value.points);
            }
4

0 回答 0