【问题标题】:C# - FireSharp Firebase Database can't retrieve dataC# - FireSharp Firebase 数据库无法检索数据
【发布时间】:2019-05-25 15:17:51
【问题描述】:

我一直在尝试使用 Windows Forms 应用程序上的 FireSharp 从 Firebase 数据库中检索所有数据,我想将其转换为自定义列表,但是当我这样做时,它给了我一个例外。

我尝试了几乎所有可能的方法,我的目标是获得这样的数据:

用户: 12345: 名称:“乔治” 姓氏:“Gigauri” 234213: 名称:“尼卡” 姓氏:“Gigauri”

作为:

12345, 234213 所以我稍后会检索他们的对象(如姓名,姓氏)。我正在使用代码:

private async void checkUser()
        {
            FirebaseResponse response = await client.GetAsync("Users");
            List<Data> list = JsonConvert.DeserializeObject<List<Data>>(response.ToJson());
        }

它给了我一个例外:

Newtonsoft.Json.JsonSerializationException
  HResult=0x80131500
  Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConnectServer.Data]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Body', line 1, position 8.
  Source=Newtonsoft.Json
  StackTrace:
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at ConnectServer.Form1.<checkUser>d__10.MoveNext() in D:\Microsoft Visual Studio\Projects\ConnectServer\ConnectServer\Form1.cs:line 101

【问题讨论】:

    标签: c# .net windows winforms firebase


    【解决方案1】:

    这里是一个以 Jason 格式检索多节点并序列化结果以填充类似控件 Datagridview 控件的示例

    Counter_class  XClass = new Counter_class();                
    FirebaseResponse firebaseResponse = await client.GetAsync("Counter/node");
    string JsTxt = response.Body;
                    if (JsTxt == "null")
                    {
    
                        return ;
                    }
    
     dynamic data = JsonConvert.DeserializeObject<dynamic>(JsTxt);
                    var list = new List<XClass >();
                    foreach (var itemDynamic in data)
                    {
                      list.Add(JsonConvert.DeserializeObject<XClass > 
                      (((JProperty)itemDynamic).Value.ToString()));
                    }
                 // Now you have a list  you can loop through to put it at any suitable Visual  
                //control 
                    foreach ( XClass  _Xcls in list)
                    {
    Invoke((MethodInvoker)delegate {
                                DataGridViewRow row(DataGridViewRow)dg.Rows[0].Clone();
                                row.Cells[0].Value =_Xdcls...
                                row.Cells[1].Value =Xdcls...
                                row.Cells[2].Value =Xdcls...
                                ......
                                dg.Insert(0, row); 
    
    }
    

    另请参阅我在此线程上的回答: How to add data from Firebase to DataGridView using FireSharp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2018-11-10
      • 1970-01-01
      相关资源
      最近更新 更多