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

问题描述 投票:2回答:1

我正在使用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();

    }
}

现在,我注意到写被调用,但读从不被调用。我像这样反序列化:

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,例外状态是:

无法投射。

c# json xna json.net
1个回答
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); 

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

© www.soinside.com 2019 - 2024. All rights reserved.