//TGD_AUDIT_STATUS、TGD_DEPT_ID都是Int32?的数据类型,如果他们的值包含小数点时直接反序列化会报错的,异常是:字符串的格式不正确,所以此时可以进行客户自定义反序列化的规则设定,这样就问题解决了。自定义实现类中,反序列化时调用ReadJson方法,序列化时调用WriteJson方法。
string json = @"[ { 'TGD_ID': '0E36DEF3-7D57-413C-8004-481DE9670503', 'TGD_PI_CODE': '33795', 'TGD_PD_NAME': '0.5ml,2D冻存管', 'TGD_PI_SPEC': '0.5ml,2D冻存管', 'TGD_PI_SPEC1': null, 'TGD_DATE': '2024-04', 'TGD_LOT': '210103791', 'TGD_EXP_DATE': '2026-07-14T00:00:00', 'TGD_PRD_DATE': '2024-01-09T00:00:00', 'TGD_UNIT': '盒', 'TGD_QUAN': 6.0, 'TGD_PRICE': 200.0, 'TGD_AMOUNT': 1200.0, 'TGD_GROUP_CODE': null, 'TGD_GROUP_NAME': null, 'TGD_AUDIT_STATUS': -1.00, 'TGD_AUDIT_DATE': '2024-05-28T15:10:17', 'TGD_AUDIT_BY': 'admin', 'TGD_AUDIT_MEMO': '1', 'TGD_IN_NO': null, 'TGD_IN_ID': null, 'TGD_INI_ID': null, 'TGD_DEPT_ID': 10.0, 'TGD_DEPT_NAME': '检验科' } ]"; var lstOutBoundByRefused2 = JsonConvert.DeserializeObject<List<b_DataSync_OutBoundByTJDX>>(json, new CustomIntConverterByNewtonsoft()); public class CustomIntConverterByNewtonsoft : Newtonsoft.Json.JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(Int32) || objectType == typeof(Int32?); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.Value != null && reader.ValueType != typeof(Int32)) return Int32.Parse(reader.Value.ToString()); return reader.Value; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if(value != null) writer.WriteValue(value.ToString()); } }
标签:AUDIT,Int32,序列化,Json,Newtonsoft,TGD,reader,null From: https://www.cnblogs.com/itjeff/p/18220216