当使用仅在大小写不同的属性反序列化JSON时,“发现模糊匹配”

时间:2015-05-06 09:13:55

标签: c# json

我班上有两个属性,如下所示。一个属性在首都,另一个属于小型。

public class Points
{
      public string X { get; set; }
      public string x { get; set; }  
}

编译好。在代码中,我反编译来自客户端的类值,如下所示:

JavaScriptSerializer serializer = new JavaScriptSerializer();
object value = serializer.Deserialize("{X:\"Car\", x:\"car\"}", typeof(Points));

在那,我得到以下例外:

{"Ambiguous match found."}
  

System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)

     

System.Type.GetProperty(String name, BindingFlags bindingAttr)

     

System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError)

     

System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)

     

System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)

     

System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)

     

System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)

     

System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)

     

System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input, Type targetType)

JSON是否反序列化为不区分大小写?如何反复区分大小写?

1 个答案:

答案 0 :(得分:3)

你是对的。 ObjectConverter.AssignToPropertyOrField(由JavaScriptSerializer内部使用)使用

type.GetProperty(memberName, BindingFlags.IgnoreCase
抛出异常的

(参见BindingFlags.IgnoreCase?),因为忽略了这种情况: - )

不要问我为什么。你无法解决这个问题。

使用Json.NET或其他Json序列化器/反序列化器。

技术上你可以:

public class DataObjectJavaScriptConverter : JavaScriptConverter
{
    private static readonly Type[] _supportedTypes = new[]
    {
        typeof( Points )
    };

    public override IEnumerable<Type> SupportedTypes
    {
        get { return _supportedTypes; }
    }

    public override object Deserialize(IDictionary<string, object> dictionary,
                                        Type type,
                                        JavaScriptSerializer serializer)
    {
        if (type == typeof(Points))
        {
            var obj = new Points();

            if (dictionary.ContainsKey("X"))
                obj.X = serializer.ConvertToType<string>(dictionary["X"]);
            if (dictionary.ContainsKey("x"))
                obj.x = serializer.ConvertToType<string>(dictionary["x"]);

            return obj;
        }

        return null;
    }

    public override IDictionary<string, object> Serialize(
            object obj,
            JavaScriptSerializer serializer)
    {
        var dataObj = obj as Points;
        if (dataObj != null)
        {
            return new Dictionary<string, object>
            {
                {"X", dataObj.X },
                {"x", dataObj.x }
            };
        }

        return new Dictionary<string, object>();
    }
}

然后

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DataObjectJavaScriptConverter() });
object value = serializer.Deserialize("{X:\"Car\", x:\"car\"}", typeof(Points));

(您为JavaScriptSerializer定义了一个自定义序列化程序/反序列化程序......代码很简单)...取自https://stackoverflow.com/a/2004216/613130(并稍加修改)