我有两个相当复杂的对象,一个是解析成功,另一个是抛出异常。对象之间的唯一区别是两个双类型属性。我将它反序列化为
var item = JsonConvert.DeserializeObject<MyItem>(result.ToString());
其中MyItem
包含两行,因此在第一种情况下,它们应该为空(item不包含它们),在第二种情况下,它们应该被设置(因为它们存在于json和反序列化对象中)。
at Newtonsoft.Json.Utilities.ConvertUtils.IntParseFast(Char[] value, Int32 start, Int32 length)
at Newtonsoft.Json.JsonTextReader.ParseNumber()
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonReader.ReadAsInt32Internal()
at Newtonsoft.Json.JsonTextReader.ReadAsInt32()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
有什么建议吗?
请根据复合对象的原语类型检查序列化对象的格式。并使用这部分代码
var item = JsonConvert.DeserializeObject<MyItem>(result.ToString(),
new JsonSerializerSettings(){DefaultValueHandling = DefaultValueHandling.Ignore});