我想从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);
}