[英]Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'
[英]Firebase Database Error: System.Collections.Generic.Dictionary`2[System.String,System.Object]
Firebase 数据库在应用程序中的其他类似操作之间需要时下载数据失败。
System.Collections.Generic.Dictionary`2[System.String,System.Object]
Firebase 在应用程序的类似操作中有时无法执行新的GetValueASync
并抛出System.Collections.Generic.Dictionary
2[System.String,System.Object]` 作为快照值。
重现步骤:使用实时数据库设置任何统一项目多次不间断执行: .GetValueAsync().ContinueWithOnMainThread(task =>
然后您将获得System.Collections.Generic.Dictionary
2[System.String,System.Object] 的值` 而不是任何子/键/数据库值。
public void aaaa() {
Reference.Child("Users").OrderByChild("About/XP").StartAt(1).LimitToFirst(12).GetValueAsync().ContinueWithOnMainThread(task => {
if (task.IsFaulted) {
return;
} else if (task.IsCompleted) {
DataSnapshot Snapshot = task.Result;
if (Snapshot != null)
Debug.Log(Snapshot.Value);
return;
}
return;
});
好吧,如果您在字典上使用ToString
(这是Debug.Log
在内部执行的操作),或者通常任何未显式实现它的类型的 object ,就会发生这种情况,它只返回与GetType().FullName
相同的内容。
Object.ToString 方法的默认实现返回对象类型的完全限定名称。
所以看起来类型是Dictionary<string, object>
。
如果您想查看所有您想做的项目,例如
foreach(var kvp in Snapshot.Value)
{
Debug.Log($"Key: {kvp.Key}, Value: {kvp.Value}");
}
但请注意:虽然 key 是string
,但 value 可能再次是未实现ToString
的类型,在这种情况下,它将再次简单地打印出类型名称。
如果您真的想打印出整个结构(只要值是可序列化的),您可以使用Newtonsoft JSON.Net并将整个字典转换为人类可读的 JSON 格式。
请参阅序列化字典
string json = JsonConvert.SerializeObject(Snapshot.Value, Formatting.Indented);
Debug.Log(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.