【发布时间】:2021-01-23 09:57:59
【问题描述】:
目前我正在使用统一发布保存在 firebase RTDB 中的 cmets。
这里是发帖代码:
Comment NewComment = new Comment("User1", "Great App!");
Dictionary<string, System.Object> childUpdates = new
Dictionary<string, System.Object>();
childUpdates["NewUpdate2"] = NewComment.ToDict();
_database.GetReference("DumbData").UpdateChildrenAsync(childUpdates);
运行良好并将数据发布到 Firebase
这里的参考是评论类。
[System.Serializable]
public class Comment
{
public Comment(string Name,string Content)
{
this.Name = Name;
this.Content = Content;
}
public Dictionary<string,System.Object> ToDict()
{
Dictionary<string, System.Object> result = new Dictionary<string, System.Object>();
result["Name"] = this.Name;
result["Content"] = this.Content;
return result;
}
public string Name;
public string Content;
}
但是当收到数据时,我会这样做
var dataSnapShot = await _database.GetReference("DumbData").GetValueAsync();
var Results = dataSnapShot.GetRawJsonValue();
var temp= JsonUtility.FromJson<Dictionary<string, Comment>>(Results)
但问题是 FromJSON 函数到处都返回 Null,尽管接收到的 JSON 与 Firebase 上的结构正确匹配,但作为参考,上面的 Results 变量如下所示:
{"NewUpdate":{"Content":"Great App!","Name":"User1"},"NewUpdate2":{"Content":"Great App!","Name":"User2"}}
这就是我被卡住的地方,我无法将响应反序列化以能够使用它。
【问题讨论】:
-
Dictionary不能被JsonUtility(反)序列化。而是使用例如Newtonsoft .NET JSON
标签: c# firebase unity3d firebase-realtime-database