gpt4 book ai didi

c# - "Ambiguous match found"反序列化 JSON 时属性仅大小写不同

转载 作者:行者123 更新时间:2023-11-30 15:26:05 25 4
gpt4 key购买 nike

我的类(class)有两个属性(property),如下所示。一处属性(property)在首都,另一处属性(property)在小。

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."}

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

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

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

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

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

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

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

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

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

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

最佳答案

你是对的。 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 (并稍作修改)

关于c# - "Ambiguous match found"反序列化 JSON 时属性仅大小写不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com