我有一个 Windows 表单应用程序,我想在其中反序列化以下 Firebase 响应。它看起来如下:
{
"Mails":
{
"A0":
{"ID":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afdac3ddc6ccc4dfdcdfc8c0efc8c2cec6c381ccc0c2" rel="noreferrer noopener nofollow">[email protected]</a>","Status":false},
"A1":
{"ID":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3657575757765958591855595b" rel="noreferrer noopener nofollow">[email protected]</a>","Status":true}
}
}
我正在使用这个:
public class Mail
{
public string ID { get; set; }
public bool Status { get; set; }
}
//This is the root object, it's going to hold a collection of the mail
public class MailCollection
{
public List<Mail> Mails { get; set; }
}
public Boolean checkValidLicense(string usermail)
{
Boolean validUser = false;
HttpWebRequest req = WebRequest.Create("https://popping-heat-1908.firebaseio.com/.json") as HttpWebRequest;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
string json = reader.ReadToEnd();
//we pass a type parameter telling it what type to deserialize to
MailCollection result = Newtonsoft.Json.JsonConvert.DeserializeObject<MailCollection>(json);
foreach (var item in result.Mails)
{
if (usermail == item.ID && item.Status)
{
validUser = true;
break;
}
}
return validUser;
}
我收到错误提示音..我现在有点沮丧..你有什么想法吗?
最佳答案
JSON 看起来更像是字典,而不是强类型对象列表。尝试反序列化为 Dictionary<string, Mail>
相反。
public class MailCollection
{
public Dictionary<string, Mail> Mails { get; set; }
}
关于时间:2019-03-17 标签:c#FIREBASEdeserialize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502865/