1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using Newtonsoft.Json;
- namespace ProductionLineMonitor.Core.Utils
- {
- public static class JsonHelper
- {
- /// <summary>
- /// 将JSON字符串反序列化成对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="baseEntity"></param>
- /// <param name="strJson"></param>
- /// <returns></returns>
- public static T ToObject<T>(T baseEntity, string strJson)
- {
- return JsonConvert.DeserializeAnonymousType(strJson, baseEntity);
- }
- /// <summary>
- /// 将Json反序列化成对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="strJson"></param>
- /// <returns></returns>
- public static T ToObject<T>(this string strJson)
- {
- return JsonConvert.DeserializeObject<T>(strJson);
- }
- /// <summary>
- /// 将对象转换层JSON字符串
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="data"></param>
- /// <returns></returns>
- public static string ToJson<T>(this T data)
- {
- return JsonConvert.SerializeObject(data);
- }
- }
- }
|