2013-05-06 46 views
0

我有2个相当复杂的对象,一个是解析成功,另一个是抛出异常。对象之间的唯一区别是2个双重属性。我反序列化作为奇怪的Json.net异常“输入字符串格式不正确。”

var item = JsonConvert.DeserializeObject<MyItem>(result.ToString()); 

MyItem拥有两条线,所以在第一种情况下,他们应该是空的(项目不追究他们),并且在第二种情况下,他们应该被设置(因为他们在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) 

有什么建议?

+4

请在每种情况下发布您正在解析的JSON('result.ToString()')。一个简短但完整的例子将是理想的。 – 2013-05-06 12:06:35

+0

发布'MyItem'类和两个JSON字符串。 – ken2k 2013-05-06 12:07:06

+0

@JonSkeet圣洁的废话,伙计们,我应该去卖一些汉堡包:)在复制diff部分时,我发现其中一个属性设置为int ... – 2013-05-06 12:12:58

回答

2

请根据复合对象的基本类型检查序列化对象的格式。并且还使用这部分代码

var item = JsonConvert.DeserializeObject<MyItem>(result.ToString(), 
new JsonSerializerSettings(){DefaultValueHandling = DefaultValueHandling.Ignore});