简体   繁体   English

反序列化 System.Collections.Generic.Dictionary

[英]deserializing System.Collections.Generic.Dictionary

My API responds with with JSON as a string, when my Unity app receives this object, I'm unsure how to deserialize it into an object from it's type of System.Collections.Generic.Dictionary , I want to make it an object of my class: PlayerInfo which is serializable. My API responds with with JSON as a string, when my Unity app receives this object, I'm unsure how to deserialize it into an object from it's type of System.Collections.Generic.Dictionary , I want to make it an object of my class:可序列化的PlayerInfo

void OnPlayerLocalUpdate(Socket socket, Packet packet, params object[] args)
{
  var myObject = JsonUtility.FromJson<PlayerInfo>(args[0].ToString());

}

The above code raises:上面的代码提出:

JSON parse error: Invalid value.   at (wrapper managed-to-native) UnityEngine.JsonUtility.FromJsonInternal(string,object,System.Type) at UnityEngine.JsonUtility.FromJson (System.String json, System.Type type)

How do I deserialize that object?如何反序列化 object?

void OnPlayerLocalUpdate(Socket socket, Packet packet, params object[] args)
{
  var serialized = JsonUtility.ToJson(args[0]);
  var myObject = JsonUtility.FromJson<PlayerInfo>(serialized);
}

First you need to serialize it then you deserialize it your expected object type.首先,您需要对其进行序列化,然后将其反序列化为您预期的 object 类型。 If args[0] is a Dictionary it should serialize to valid json object.如果 args[0] 是一个字典,它应该序列化为有效的 json object。 Another way of achieving this is to initialize your PlayerInfo manually.实现此目的的另一种方法是手动初始化您的 PlayerInfo。 Then use reflection to set its' properties.然后使用反射来设置它的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Collections.Generic.Dictionary =终极表现? - System.Collections.Generic.Dictionary = Ultimate performance? System.Collections.Generic.Dictionary foreach顺序 - System.Collections.Generic.Dictionary foreach order 将System.Collections.Generic.Dictionary转换为XDocument - Turn a System.Collections.Generic.Dictionary into an XDocument &#39;System.Collections.Generic.Dictionary的最佳重载方法匹配 <int,System.Collections.Generic.Dictionary<string,int> &gt; .Dictionary(int)&#39; - The best overloaded method match for 'System.Collections.Generic.Dictionary<int,System.Collections.Generic.Dictionary<string,int>>.Dictionary(int)' 通过for语句循环通过System.Collections.Generic.Dictionary - Loop through System.Collections.Generic.Dictionary via for statement LINQ to Entities无法识别方法&#39;System.Collections.Generic.Dictionary` - LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary` 如何在 Realm-dotnet 中存储 System.Collections.Generic.Dictionary - How to store a System.Collections.Generic.Dictionary in realm-dotnet 枚举System.Collections.Generic.Dictionary中的键<string,string> - enumerate keys in in a System.Collections.Generic.Dictionary<string,string> System.Collections.Generic.Dictionary`Add`vs set&#39;Item` - System.Collections.Generic.Dictionary `Add` vs set `Item` 无法将类型字符串隐式转换为System.Collections.Generic.Dictionary <string,System.Collections.Generic.Dictionary><string,object> &gt; - Cannot implicitly convert type string to System.Collections.Generic.Dictionary<string,System.Collections.Generic.Dictionary><string,object>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM