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

问题描述 投票:0回答:1

我有一个客户列表,我想将它们推入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#的新手,怜悯我:)请提前;

c# json.net firebase deserialization
1个回答
2
投票

回答更新的问题

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.