问题描述
[Serializable]
public struct InventoryData
{
public int a;
public BackpackData Backpack;
public EquipmentData Equipment;
}
[Serializable]
public struct BackpackData
{
public int[] Amounts;
public int[] Items;
}
[Serializable]
public struct EquipmentData
{
public int Boots;
public int gloves;
public int Helmet;
public Pocket LeftPocket;
public int PrimaryWeapon;
public Pocket RightPocket;
public int SecondaryWeapon;
public int Trousers;
public int Vest;
}
[Serializable]
public struct Pocket
{
public int Amount;
public int Item;
}
我使用 Firebase,数据返回为 dataSnaphsot。
public void Load(ICanLoadData sender,string path = "")
{
DataSnapshot dataSnapshot = null;
DatabaseReference databaseReference;
if (path == "")
{
databaseReference = firebaseDatabase.RootReference.Child("users")
.Child(FirebaseComponent.firebaseUser.UserId);
}
else
databaseReference = firebaseDatabase.RootReference.Child("users")
.Child(FirebaseComponent.firebaseUser.UserId).Child(path);
databaseReference.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
sender.OnLoadProcessFaulted();
if (task.IsCanceled)
sender.OnLoadProcessCanceled();
if (!task.IsCompleted) return;
dataSnapshot = task.Result;
if (dataSnapshot.Exists)
sender.OnLoadProcessCompleted(dataSnapshot);
else
sender.OnLoadProcessDatanotExists();
});
}
我想从数据库中检索的数据结构如图所示。
public void LoadData(DataSnapshot dataSnapshot)
{
try
{
inventoryData = JsonUtility.FromJson<InventoryData>(dataSnapshot.ToString());
}
catch (Exception e)
{
Debug.Log(e.Message);
throw;
}
}
这是我用来通过提供数据快照从 JSON 获取数据的方法。快照返回正确的数据。但是程序在这一行停止。发生错误并指出,json 解析错误:无效值。
JSON parse error: Invalid value.
UnityEngine.Debug:Log (object)
ZombieShooter.Component.InventoryComponent:LoadData (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/InventoryComponent.cs:88)
ZombieShooter.Component.DataComponent:OnLoadProcessCompleted (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/DataComponent.cs:223)
ZombieShooter.Component.DatabaseComponent/<>c__displayClass5_0:<Load>b__0 (System.Threading.Tasks.Task`1<Firebase.Database.DataSnapshot>) (at Assets/Game Assets/Game/Components/Database/DatabaseComponent.cs:58)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()
如您所见,加载数据方法的结果是正确的。
在我看来,问题与结构类型有关。我使用嵌套数据,这种数据类型是结构。 JSON 可能无法将字符串解析为结构类型。在这种情况下,我应该使用哪种替代方案?
这就是数据在数据库中的样子。
Debug.log(dataSnapshot.ToString()
的结果
DataSnapshot { key = Inventory,value = System.Collections.Generic.Dictionary`2[System.String,System.Object] }
UnityEngine.Debug:Log (object)
ZombieShooter.Component.InventoryComponent:LoadData (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/InventoryComponent.cs:84)
ZombieShooter.Component.DataComponent:OnLoadProcessCompleted (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/DataComponent.cs:223)
ZombieShooter.Component.DatabaseComponent/<>c__displayClass5_0:<Load>b__0 (System.Threading.Tasks.Task`1<Firebase.Database.DataSnapshot>) (at Assets/Game Assets/Game/Components/Database/DatabaseComponent.cs:58)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)