JSON序列化和反序列化 对decmail 取值问题
地图API返回经纬度:经度: 纬度:
lng":114.03483089395202,"lat":22.615589046911805
decmail 接收数据后两位会截掉 系统框架默认取得是double类型
解决方式是 进行设置 json.FloatParseHandling = FloatParseHandling.Decimal; setting.FloatParseHandling = FloatParseHandling.Decimal; 即可
/// <summary>
/// JSON对象反序列化
/// </summary>
/// <param name="JSON"></param>
/// <returns></returns>
public static T DeserializeObject<T>(string JSON)
{
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
json.FloatParseHandling = FloatParseHandling.Decimal;
json.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
json.DateFormatString = "yyyy-MM-dd HH:mm:ss";
StringReader sr = new StringReader(JSON);
Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
T result = (T)json.Deserialize(reader, typeof(T));
reader.Close();
return result;
}
/// <summary>
/// 序列化为JSON
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string SerializeObject(object value, bool NullValueIgnore = true)
{
JsonSerializerSettings setting = new JsonSerializerSettings();
setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
setting.FloatParseHandling = FloatParseHandling.Decimal;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
setting.Converters.Add(EnumConverter);
//空值处理
if (NullValueIgnore)
setting.NullValueHandling = NullValueHandling.Ignore;
/// 序列化为JSON
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string SerializeObject(object value, bool NullValueIgnore = true)
{
JsonSerializerSettings setting = new JsonSerializerSettings();
setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
setting.FloatParseHandling = FloatParseHandling.Decimal;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
setting.Converters.Add(EnumConverter);
//空值处理
if (NullValueIgnore)
setting.NullValueHandling = NullValueHandling.Ignore;
//包含实体默认值
setting.DefaultValueHandling = DefaultValueHandling.Include;
setting.DefaultValueHandling = DefaultValueHandling.Include;
return JsonConvert.SerializeObject(value, Formatting.Indented, setting);
}