时间:2019-03-17 标签:c#FIREBASEdeserialize

标签 c# json deserialization

我有一个 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/

相关文章:

javascript - 实时更改 Repeater 内的标签

c# - 防止重复注册 - CaSTLe Windsor

javascript - 访问单个 JSON 对象数组中的值

javascript - jQuery/JavaScript 循环遍历 json 数据结果

java - 序列化和写入对象

c# - 是否可以保证 AsyncCallbacks 始终在主线程上执行?

c# - 如何修复我的后台解决方案以进行线程安全调用?

javascript - 如何在禁用的 asp.net 下拉列表中添加警报?

c# - 有没有更好的方法通过套接字发送 int 列表?

java - Java中的序列化顺序