47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace KJD_Web.Utils
|
|
{
|
|
public class JsonUtils
|
|
{
|
|
/// <summary>
|
|
/// 反序列化Json字符串生成对象实体
|
|
/// </summary>
|
|
/// <typeparam name="T">对象类型</typeparam>
|
|
/// <param name="jsonStr">json字符串</param>
|
|
/// <returns>对象实体</returns>
|
|
public static T DeserializeJsonToObject<T>(string jsonStr) where T : class
|
|
{
|
|
try
|
|
{
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
StringReader sr = new StringReader(jsonStr);
|
|
object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
|
|
T t = obj as T;
|
|
return t;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static string SerializerObjectToString(object obj)
|
|
{
|
|
try
|
|
{
|
|
string str = JsonConvert.SerializeObject(obj, Formatting.None);
|
|
return str;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
} |