c#FireBase生成的字符串索引反序列化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#FireBase生成的字符串索引反序列化相关的知识,希望对你有一定的参考价值。

我有一个客户列表,我想将它们推入firebase,我正在使用firesharp,但是当我这样做时,firebase为我推送的每个客户端创建一个字符串索引。

问题是,当我想要数据时,由于字符串索引和List类,我无法反序列化它。

这就是我所拥有的:

 public class Client
{
    public string name { get; set; }
    public string lastname { get; set; }
    public int age { get; set; }
    public string phone { get; set; }
    public string mail { get; set; }
    public string adress { get; set; }
    public List kids { get; set; }


    public Cliente(string nameArg, string lastnameArg, int ageArg, string phoneArg, string mailArg, string adressArg)
    {
        name = nameArg;
        lastname = lastnameArg;
        age = ageArg;
        phone = phoneArg;
        mail = mailArg;
        adress = adressArg;
        kids = new List<kids>();
    }
}

然后我做了一个班级来接待这个客户(不确定这是一个好主意,欢迎任何建议)

 public class ClientList
{
    public List<Client> clientList
    { get; set; }
}

所以我将Client对象推送到firebase中的clientList,如下所示:

public async void pushClient(Client c) {
        PushResponse response = await client.PushAsync("clientList", c);
        MessageBox.Show (response);//i use this line to check the index
    }

响应总是像“Ahs74-djAdie8”< - 这是firebase给我的Client对象的索引。

firebase json在推送后看起来像这样:

{"clientList":{
    "Gfjy654-hfGfgv":{
        "name":"asasasas",
        "lastname":"ooooooo",
        "age":"22",
        "mail":"hhhh@hhh",
        "phone":"+665544581"
    },
    "H5j7su-UHudufu":{
        name:"asasasas",
        "lastname":"ooooooo",
        "age":"22",
        "mail":"hhhh@hhh",
        "phone":"+665544581"
    }
}
}

现在,当我试图获取此数据并反序列化它不起作用时,这是方法和异常:

    private async void getClients()
    {
        FirebaseResponse response = await client.GetAsync("");
        list = response.ResultAs<ClientList>();
    }

Newtonsoft.Json.dll中出现“Newtonsoft.Json.JsonSerializationException”类型的异常,但未在用户代码中处理

我测试了将firebase中的索引手动更改为数字并立即工作,但我不想这样做,我只是想反序列化它!

我的线索是:列表类不适用于字符串索引;我是c#的新手,怜悯我:)请提前;

答案

回答更新的问题

您需要将clientList更改为字典而不是列表:

    public class ClientListResponse
    {
        public Dictionary<string, Client> clientList { get; set; }
    }

这样做之后,理论上你显示的JSON现在应该被成功反序列化,字典键是Gfjy654-hfGfgv字符串。

以上是关于c#FireBase生成的字符串索引反序列化的主要内容,如果未能解决你的问题,请参考以下文章

Android Parcelable反序列化报错笔记:java.lang.RuntimeException: Unmarshalling unknown type code at offset(代码片

2021-08-04:给定一个字符串str,当然可以生成很多子序列。返回有多少个子序列是回文子序列,空序列不算回文。比如,str = “aba”,回文子序列:{a}{a} {a,a} {b}{(代码片

将cXML反序列化为C#类

Firebase 评论查看错误 - 反序列化时需要一个列表,但有一个类 java.util.HashMap

将 Firebase 数据快照反序列化为 Kotlin 数据类

通讯协议及Google.Protobuf生成c#代码 序列及反序列化