using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
using System.Collections;
using Newtonsoft.Json;
using System.IO;
namespace ProductReg
{
public class JsonHelper
{
//序列化
public static string SerializeObject(ConfigFileModel cfm)
{
return JsonConvert.SerializeObject(cfm);
}
//反序列化
public static ConfigFileModel DeserializeObject(string js1)
{
ConfigFileModel debc1 = JsonConvert.DeserializeObject<ConfigFileModel>(js1);
return debc1;
}
public static string filepath()
{
string serverAppPath = AppDomain.CurrentDomain.BaseDirectory;
string con_file_path = serverAppPath + "config.json";
if (!File.Exists(con_file_path))
{
File.Create(con_file_path);
}
return con_file_path;
}
//把模型数据写到文件
public static void writejsonfile(List<ConfigFileModel> cfm)
{
string con_file_path = filepath();
using (StreamWriter sw = new StreamWriter(con_file_path))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
JsonWriter writer = new JsonTextWriter(sw);
//把模型数据序列化并写入Json.net的JsonWriter流中
serializer.Serialize(writer, cfm);
//ser.Serialize(writer, ht);
writer.Close();
sw.Close();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
//集合写入json
public static void writeList2jsonfile(Hashtable ht)
{
string con_file_path = filepath();
using (StreamWriter sw = new StreamWriter(con_file_path))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
JsonWriter writer = new JsonTextWriter(sw);
//把模型数据序列化并写入Json.net的JsonWriter流中
serializer.Serialize(writer, ht);
writer.Close();
sw.Close();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
//读取json文件
public static void readjsonfile(ref List<ConfigFileModel> cfm)
{
//List<ConfigFileModel> list = new List<ConfigFileModel>();
string con_file_path = filepath();
using (StreamReader sr = new StreamReader(con_file_path))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
JsonReader reader = new JsonTextReader(sr);
//对读取出的Json.net的reader流进行反序列化,并装载到模型中
cfm = serializer.Deserialize<List<ConfigFileModel>>(reader);
//list.Add(cfm);
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
// return list;
}
}
public class ConfigFileModel
{
public ConfigFileModel() { }
string _typeName;
public string TypeName
{
get { return _typeName; }
set { _typeName = value; }
}
string _createDate;
public string CreateDate
{
get { return _createDate; }
set { _createDate = value; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
using System.Collections;
using Newtonsoft.Json;
using System.IO;
namespace ProductReg
{
public class JsonHelper
{
//序列化
public static string SerializeObject(ConfigFileModel cfm)
{
return JsonConvert.SerializeObject(cfm);
}
//反序列化
public static ConfigFileModel DeserializeObject(string js1)
{
ConfigFileModel debc1 = JsonConvert.DeserializeObject<ConfigFileModel>(js1);
return debc1;
}
public static string filepath()
{
string serverAppPath = AppDomain.CurrentDomain.BaseDirectory;
string con_file_path = serverAppPath + "config.json";
if (!File.Exists(con_file_path))
{
File.Create(con_file_path);
}
return con_file_path;
}
//把模型数据写到文件
public static void writejsonfile(List<ConfigFileModel> cfm)
{
string con_file_path = filepath();
using (StreamWriter sw = new StreamWriter(con_file_path))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
JsonWriter writer = new JsonTextWriter(sw);
//把模型数据序列化并写入Json.net的JsonWriter流中
serializer.Serialize(writer, cfm);
//ser.Serialize(writer, ht);
writer.Close();
sw.Close();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
//集合写入json
public static void writeList2jsonfile(Hashtable ht)
{
string con_file_path = filepath();
using (StreamWriter sw = new StreamWriter(con_file_path))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
JsonWriter writer = new JsonTextWriter(sw);
//把模型数据序列化并写入Json.net的JsonWriter流中
serializer.Serialize(writer, ht);
writer.Close();
sw.Close();
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
//读取json文件
public static void readjsonfile(ref List<ConfigFileModel> cfm)
{
//List<ConfigFileModel> list = new List<ConfigFileModel>();
string con_file_path = filepath();
using (StreamReader sr = new StreamReader(con_file_path))
{
try
{
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
JsonReader reader = new JsonTextReader(sr);
//对读取出的Json.net的reader流进行反序列化,并装载到模型中
cfm = serializer.Deserialize<List<ConfigFileModel>>(reader);
//list.Add(cfm);
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
// return list;
}
}
public class ConfigFileModel
{
public ConfigFileModel() { }
string _typeName;
public string TypeName
{
get { return _typeName; }
set { _typeName = value; }
}
string _createDate;
public string CreateDate
{
get { return _createDate; }
set { _createDate = value; }
}
}
}