记录操作Json的方法
class JsonHelper
{
/// 将对象序列化为json文件
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="t">实例</param>
/// <param name="path">存放路径</param>
public static void ObjectToJson<T>(T t, string path) where T : class
{
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
{
formatter.WriteObject(stream, t);
}
}
/// <summary>
/// json字符串转成对象
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="json">json格式字符串</param>
/// <returns>对象</returns>
public static T JsonToObject<T>(T t, string path) where T : class
{
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
using (FileStream stream = new FileStream(path, FileMode.Open))
{
T result = formatter.ReadObject(stream) as T;
return result;
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private static string GetFileContent(string filePath)
{
string result = "";
if (File.Exists(filePath))
{
result = File.ReadAllText(filePath);
}
return result;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <param name="path"></param>
/// <returns></returns>
public static T Deserialize<T>(T t, string path)
{
string fileContent = GetFileContent(path);
if (!string.IsNullOrWhiteSpace(fileContent))
{
//反序列化
t = JsonConvert.DeserializeObject<T>(fileContent);
}
return t;
}
/// <summary>
/// 读取JSON文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public static T ReadJSON<T>(string path)
{
try
{
StreamReader streamReader = new StreamReader(path);
dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(streamReader.ReadToEnd());
streamReader.Close();
return (T)jsonObj;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return default;
}
/// <summary>
/// 写入JSON文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <param name="value"></param>
public static void WriteJSON<T>(string path, T value)
{
try
{
//序列化为json时过滤掉NULL
var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
string output = JsonConvert.SerializeObject(value, Formatting.Indented, jsonSetting);
File.WriteAllText(path, output);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 修改JSON文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void EditJSON<T>(string path, string key, T value)
{
try
{
//序列化为json时过滤掉NULL
var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
//读取文件
StreamReader streamReader = new StreamReader(path);
dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(streamReader.ReadToEnd());
//修改值
jsonObj[key] = value;
streamReader.Close();
string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented, jsonSetting);
File.WriteAllText(path, output);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}