Newtonsoft.Json

1、序列化

string jsonStr = JsonConvert.SerializeObject(obj)

2、反序列化

T obj = JsonConvert.DeserializeObject(jsonStr)

3、Newtonsoft.Json 扩展方法类

/// <summary>
/// Newtonsoft.Json的扩展方法类
/// </summary>
public static class NewtonsoftJsonExtension
{
    /// <summary>
    /// 将Json字符串反序列化为对象实例——Newtonsoft.Json
    /// </summary>
    /// <typeparam name="T">对象类型</typeparam>
    /// <param name="jsonString">Json字符串</param>
    /// <returns>对象实例</returns>
    public static T DeserializeObjectFromJson_NJ<T>(this string jsonString)
    {
        return JsonConvert.DeserializeObject<T>(jsonString);
    }

    /// <summary>
    /// 将对象实例序列化为Json字符串——Newtonsoft.Json
    /// </summary>
    /// <typeparam name="T">对象类型</typeparam>
    /// <param name="obj">对象实例</param>
    /// <returns>Json字符串</returns>
    public static string SerializeObjectToJson_NJ<T>(this T obj)
    {
        return JsonConvert.SerializeObject(obj);
    }
}
posted @ 2022-08-20 10:17  his365  阅读(38)  评论(0编辑  收藏  举报