C# 集合 序列化 反序列化

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; }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值