[英]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.