对象类型' Newtonsoft.Json.Linq.JObject'无法转换为类型

时间:2015-04-01 20:03:14

标签: c# json xna json.net

我正在使用JSON.NET使用反射将一些数据解析为某些特定属性。

property.SetValue(comInstance, field.Value);

这一行引发ArgumentException

  

mscorlib.dll中出现未处理的“System.ArgumentException”类型异常

     

其他信息:“Newtonsoft.Json.Linq.JObject”类型的对象无法转换为“Microsoft.Xna.Framework.Vector2”类型。

我知道这必须与我的JsonConverter Vector2类型有关,如下所示:

public class Vector2Converter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Vector2));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jsonObject = JObject.Load(reader);
        var properties = jsonObject.Properties().ToList();
        return new Vector2
        {
            X = (float)properties[0].Value,
            Y = (float)properties[1].Value
        };
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        Vector2 v = (Vector2)value;
        writer.WriteStartObject();
        writer.WritePropertyName("X");
        serializer.Serialize(writer, v.X);
        writer.WritePropertyName("Y");
        serializer.Serialize(writer, v.Y);
        writer.WriteEndObject();

    }
}

现在,我注意到write被调用了,但是read永远不会被调用。 我像这样反序列化:

JsonConvert.DeserializeObject<EntityJson>(json, JsonUtility.Settings);

JsonUility.Settings包含Vector2Converter

我按照这样序列化:

JsonConvert.SerializeObject(entityJson, Formatting.Indented, JsonUtility.Settings);

JSON中的字段本身也很好看:

"Center": {
      "X": 116.0,
      "Y": 65.0
    },

任何人都可以帮助我吗?

编辑:

internal class EntityJson
{
    public string Name { get; set; }
    public Dictionary<string, ComponentJson> Components { get; set; }

    public EntityJson()
    {
        Components = new Dictionary<string, ComponentJson>();
    }

    public Entity ToEntity()
    {
        Entity entity = new Entity(Name);

        foreach(KeyValuePair<string, ComponentJson> com in Components)
        {
            ObjectHandle handle = Activator.CreateInstance(com.Value.Namespace, com.Value.Namespace +"."+ com.Key);
            Object handleValue = handle.Unwrap();
            Component comInstance = (Component)handleValue;

            foreach(KeyValuePair<string, object> field in com.Value.Fields)
            {
                PropertyInfo property = comInstance.GetType().GetProperty(field.Key, 
                    BindingFlags.NonPublic | 
                    BindingFlags.Public | 
                    BindingFlags.Instance);

                property.SetValue(comInstance, field.Value); // <-- field.Value
            }

            entity.AddUnsafe(comInstance);
        }

        return entity;
    }
}

field.value将是JObject而不是Vector2,例外情况说明:

  

它不能被铸造。

1 个答案:

答案 0 :(得分:0)

好的,我已经成功解决了这个问题:

object result = field.Value;
if (field.Value.GetType() == typeof(JObject))
{
      JToken token = (JToken)field.Value;
      result = token.ToObject(property.PropertyType, JsonSerializer.Create(JsonUtility.Settings));
}
else
{
      result = Convert.ChangeType(result, property.PropertyType);
}

property.SetValue(comInstance, result); 

这是一个稳定的解决方案吗?

相关问题