奇怪的 Json.net 异常“输入字符串的格式不正确。”

Vit*_*nko 2 c# parsing json.net windows-phone-7

我有 2 个相当复杂的对象,一个正在解析成功,另一个正在抛出异常。对象之间的唯一区别是 2 个双精度型属性。我将其反序列化为

var item = JsonConvert.DeserializeObject<MyItem>(result.ToString());
Run Code Online (Sandbox Code Playgroud)

其中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)
Run Code Online (Sandbox Code Playgroud)

有什么建议么?

Naz*_*zyk 5

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

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