.NET 生成JSON 文件

//构成配置文件路径
string con_file_path = @"" + serverAppPath + pId + @".json";
if (File.Exists(con_file_path))
{
File.Delete(con_file_path);

}
FileStream fs = File.Create(con_file_path);
fs.Close();
fs.Dispose();
//把模型数据写到文件
FileStream _file = new FileStream(con_file_path, FileMode.Create, FileAccess.ReadWrite);
using (StreamWriter sw = new StreamWriter(_file))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;

//构建Json.net的写入流
JsonWriter writer = new JsonTextWriter(sw);
//把模型数据序列化并写入Json.net的JsonWriter流中
serializer.Serialize(writer, model);
writer.Close();
sw.Close();
}
catch (Exception ex)
{
ex.Message.ToString();
}

}

猜你喜欢

转载自www.cnblogs.com/jemeryjie/p/10064977.html