上代码:
/// <summary> /// 对象序列化及反序列化帮助类 /// </summary> public class JsonConvertHelper { public static string SerializeObject(object valueObject) { return JsonConvert.SerializeObject(valueObject, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }); } public static T DeserializeObject<T>(string objectJsonString) { if (!string.IsNullOrEmpty(objectJsonString)) { return JsonConvert.DeserializeObject<T>(objectJsonString); } else { return default(T); } } /// <summary> /// 反序列化对象 /// </summary> /// <param name="objectJsonString"></param> /// <param name="targetType"></param> /// <returns></returns> public static object DeserializeObject(object objectJsonString, Type targetType) { object output = null; if (objectJsonString != null) { if (VerificatBaseObject(targetType)) { output = Convert.ChangeType(objectJsonString, targetType); } else { Type objectType = objectJsonString.GetType(); if (targetType.IsGenericType) { if (objectType.FullName == typeof(JArray).FullName || objectType.FullName == typeof(System.Collections.ArrayList).FullName) { List<object> objectList = DeserializeObject<List<object>>(SerializeObject(objectJsonString)); if (objectList.Count > 0) { Type GenericArgumentType = targetType.GetGenericArguments()[0]; object objectTypeList = Activator.CreateInstance(targetType); for (var i = 0; i < objectList.Count; i++) { targetType.GetMethod("Add").Invoke(objectTypeList, new object[] { DeserializeObject(SerializeObject(objectList[i]), GenericArgumentType) }); } output = objectTypeList; } } else { if (VerificatBaseObject(targetType)) { output = Convert.ChangeType(objectJsonString, targetType); } else { output = DeserializeObject(SerializeObject(objectJsonString), targetType); } } } else if (targetType.IsArray) { if (objectType.FullName == typeof(System.Collections.ArrayList).FullName) { List<object> objectList = DeserializeObject<List<object>>(SerializeObject(objectJsonString)); if (objectList.Count > 0) { Type GenericArgumentType = targetType.GetGenericArguments()[0]; Array objectTypeList = Array.CreateInstance(GenericArgumentType, objectList.Count); for (var i = 0; i < objectList.Count; i++) { objectTypeList.SetValue(DeserializeObject(SerializeObject(objectList[i]), GenericArgumentType), i); } output = objectTypeList; } } } else { output = JsonConvert.DeserializeObject(SerializeObject(objectJsonString), targetType); } } } return output; } /// <summary> /// 这边不是单纯的判断基础类,判断将需要转化的类型 /// </summary> /// <param name="targetType"></param> /// <returns></returns> public static bool VerificatBaseObject(Type targetType) { if (targetType.FullName == typeof(String).FullName || targetType.FullName == typeof(Int32).FullName || targetType.FullName == typeof(Int32?).FullName || targetType.FullName == typeof(Int64).FullName || targetType.FullName == typeof(Int64?).FullName || targetType.FullName == typeof(Boolean).FullName || targetType.FullName == typeof(Boolean?).FullName || targetType.FullName == typeof(Int16).FullName || targetType.FullName == typeof(Int16?).FullName || targetType.FullName == typeof(double).FullName || targetType.FullName == typeof(double?).FullName || targetType.FullName == typeof(float).FullName || targetType.FullName == typeof(float?).FullName || targetType.FullName == typeof(decimal).FullName || targetType.FullName == typeof(decimal?).FullName || targetType.FullName == typeof(DateTime).FullName || targetType.FullName == typeof(DateTime?).FullName) { return true; } return false; } }