微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase C#/ Xamarin反序列化错误

如何解决Firebase C#/ Xamarin反序列化错误

我正在尝试从Firebase读取数据。我的沟通正确,并且能够写到Firebase。出于某种原因,我遇到了反序列化错误,经过反复试验,我仍然无法克服它。我的目标是从Firebase读取数据并返回卡对象列表。这是数据在Firebase中的存储方式:

enter image description here

我有一个叫做Card的简单类:请看下面的

using System;
using System.Collections.Generic;
using System.Text;

namespace Jorbozeh.Model
{
    public class Card
    {
        public string CardTitle { get; set; }
        public string CardDesc { get; set; }
        public string CardSubDesc { get; set; }
        public string CardImage { get; set; }

        public override string ToString()
        {
            return $"Id: {1},Title: {CardTitle},Desc: {CardDesc},SubDesc: {CardSubDesc}";
        }
    }
}

这是我在主CS文件调用firebase函数的方式。

       public async Task<List<Card>> GetAllCards()
        {
                return (await firebase
                  .Child("Simple")
                  .OnceAsync<Card>()).Select(item => new Card
                  {
                      CardDesc = (string)item.Object.CardDesc,CardSubDesc = (string)item.Object.CardSubDesc,CardImage = (string)item.Object.CardImage,CardTitle = (string)item.Object.CardTitle
                  }).ToList();
        }

以下是我关于反序列化的错误。它可能无法将对象序列化为对象,因为它混淆了列表和卡obj。即使在网上搜索了很多内容,也不确定如何解决

10-16 15:03:00.501 I/mono-stdout(26922): Firebase.Database.FirebaseException: Exception occured while processing the request.
Firebase.Database.FirebaseException: Exception occured while processing the request.
Url: https://jorbozeh-6ae59.firebaseio.com/Simple/.json
Request Data: 
10-16 15:03:00.509 I/mono-stdout(26922): Url: https://jorbozeh-XXXXX.firebaseio.com/Simple/.json
10-16 15:03:00.509 I/mono-stdout(26922): Request Data: 
10-16 15:03:00.509 I/mono-stdout(26922): Response: [{"CardDesc":"0D","CardImage":"0i","CardSubDesc":"0sub","CardTitle":"0t"},{"CardDesc":"1desc","CardImage":"1image","CardSubDesc":"1subdesc","CardTitle":"1title"},{"CardDesc":"2desc","CardImage":"2image","CardSubDesc":"2subdesc","CardTitle":"2title"}] ---> Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Jorbozeh.Model.Card]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
Response: [{"CardDesc":"0D",Jorbozeh.Model.Card]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection,IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '',line 1,position 1.
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (Newtonsoft.Js
on.JsonReader reader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract) [0x00058] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader,Newtonsoft.Json.Serialization.JsonContract contract,Newtonsoft.Json.Serialization.JsonProperty member,System.Object existingValue,System.String id) [0x00012] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader,Newtonsoft.Json.Serialization.JsonContainerContract containerContract,Newtonsoft.Json.Serialization.JsonProperty containerMember,System.Object existingValue) [0x0007f] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (N
ewtonsoft.Json.JsonReader reader,System.Boolean checkAdditionalContent) [0x000db] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader,System.Type objectType) [0x00054] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader,System.Type objectType) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value,System.Type type,Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value,Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.510 I/mono-stdout(26922): To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection,IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
10-16 15:03:00.510 I/mono-stdout(26922): Path '',position 1.
10-16 15:03:00.510 I/mono-stdout(26922):   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (Newtonsoft.Json.JsonReader reader,Newtonsoft.Json.Serialization.JsonContract contract) [0x00058] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.510 I/mono-stdout(26922):   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader,System.String id) [0x00012] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.511 I/mono-stdout(26922):   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader,System.Object existingValue) [0x0007f] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.511 I/mono-stdout(26922):   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader,System.Boolean checkAdditionalContent) [0x000db] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.511 I/mono-stdout(26922):   at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader,System.Type objectType) [0x00054] in <d32db49e5e3440729da31845c03ddc3a>:0 
  at Firebase.Database.Http.HttpClientExtensions.GetobjectCollectionAsync[T] (System.Net.Http.HttpClient client,System.String requestUri,Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings) [0x00140] in <2cf89c22afe1453abece5a4e4d8b1e59>:0 
   --- End of inner exception stack trace ---
  at Firebase.Database.Http.HttpClientExtensions.GetobjectCollectionAsync[T] (System.Net.Http.HttpClient client,Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings) [0x001ab] in <2cf89c22afe1453abece5a4e4d8b1e59>:0 
  at Firebase.Database.Query.FirebaseQuery.OnceAsync[T] (System.Nullable`1[T] timeout) [0x00129] in <2cf89c22afe1453abece5a4e4d8b1e59>:0 
  at Jorbozeh.Helper.FirebaseHelper.GetFirstCard () [0x00041] in C:\Users\aehsa\source\repos\Jorbozeh\Jorbozeh\Helper\FirebaseHelper.cs:29  Exception caught.10-16 15:03:00.511 I/mono-stdout(26922):   at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader,System.Type objectType) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 

10-16 15:03:00.512 I/mono-stdout(26922):   at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value,Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.512 I/mono-stdout(26922):   at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value,Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <d32db49e5e3440729da31845c03ddc3a>:0 
10-16 15:03:00.512 I/mono-stdout(26922):   at Firebase.Database.Http.HttpClientExtensions.GetobjectCollectionAsync[T] (System.Net.Http.HttpClient client,Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings) [0x00140] in <2cf89c22afe1453abece5a4e4d8b1e59>:0 
10-16 15:03:00.512 I/mono-stdout(26922):    --- End of inner exception stack trace ---
10-16 15:03:00.513 I/mono-stdout(26922):   at Firebase.Database.Http.HttpClientExtensions.GetobjectCollectionAsync[T] (System.Net.Http.HttpClient client,Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings) [0x001ab] in <2cf89c22afe1453abece5a4e4d8b1e59>:0 
10-16 15:03:00.513 I/mono-stdout(26922):   at Firebase.Database.Query.FirebaseQuery.OnceAsync[T] (System.Nullable`1[T] timeout) [0x00129] in <2cf89c22afe1453abece5a4e4d8b1e59>:0 
10-16 15:03:00.513 I/mono-stdout(26922):   at Jorbozeh.Helper.FirebaseHelper.GetFirstCard () [0x00041] in C:\Users\aehsa\source\repos\Jorbozeh\Jorbozeh\Helper\FirebaseHelper.cs:29  Exception caught.

请让我知道如何调整代码以使通话正常工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。