C# 对象集合转json格式

string[] sArrBoxInOut = new string[2];
            sArrBoxInOut[0] = "2,2019-03-29 09:38:52,2019-03-29,0,0,5,用户名,0,,,,,,";
            sArrBoxInOut[1] = "3,2019-03-29 09:39:39,2019-03-29,0,0,2,用户名,0,,,,,,";
            string[] str = null;
            string strs = "[";
            List<BoxInOutModel> BoxInOutModels = new List<BoxInOutModel>();
            for (int i = 0; i < sArrBoxInOut.Length; i++)
            {
                str = sArrBoxInOut[i].Split(',');
                BoxInOutModel box = new BoxInOutModel()
                {
                    RecSeqNo = str[0],
                    OperTime = str[1],
                    OperDate = str[2],
                    OperType = str[3],
                    OperStatus = str[4],
                    BoxNo = str[5],
                    OperID = str[6],
                    RecStatus = str[7],
                    CourtID = str[8],
                    UserName = str[9],
                    CardID = str[10],
                    LawyerCard = str[11],
                    Purpose = str[12],
                    Mobile = str[13]
                };
                BoxInOutModels.Add(box);
            }
            
            JsonSerializer serializer = new JsonSerializer(); 
            StringWriter sw = new StringWriter(); 
            serializer.Serialize(new JsonTextWriter(sw), BoxInOutModels);
            string result = sw.GetStringBuilder().ToString();
            MessageBox.Show(result);

类BoxInOutModel,注意这个需要和json里的数据字段个数完全一致

  public class BoxInOutModel
    {
        string recSeqNo = "";//记录序号
        string operTime = "";//操作时间
        string operDate = "";//操作日期
        string operStatus = "";//操作状态
        string boxNo = "";//柜箱号
        string operID = "";//使用人
        string recStatus = "";//记录状态
        string courtID = "";//法院ID
        string userName = "";//姓名
        string cardID = "";//证件号码
        string lawyerCard = "";//律师号码
        string purpose = "";//采访事由
        string mobile = "";//手机号码 
        string operType = "";//存取1存入2取出

        #region prop
        /// <summary>
        /// 记录序号
        /// </summary>
        public string RecSeqNo { get => recSeqNo; set => recSeqNo = value; }
        /// <summary>
        /// 操作时间
        /// </summary>
        public string OperTime { get => operTime; set => operTime = value; }
        /// <summary>
        /// 操作日期
        /// </summary>
        public string OperDate { get => operDate; set => operDate = value; }
        /// <summary>
        /// 操作状态
        /// </summary>
        public string OperStatus { get => operStatus; set => operStatus = value; }
        /// <summary>
        /// 柜箱号
        /// </summary>
        public string BoxNo { get => boxNo; set => boxNo = value; }
        /// <summary>
        /// 使用人
        /// </summary>
        public string OperID { get => operID; set => operID = value; }
        /// <summary>
        /// 记录状态
        /// </summary>
        public string RecStatus { get => recStatus; set => recStatus = value; }
        /// <summary>
        /// 法院ID
        /// </summary>
        public string CourtID { get => courtID; set => courtID = value; }
        /// <summary>
        /// 姓名
        /// </summary>
        public string UserName { get => userName; set => userName = value; }
        /// <summary>
        /// 证件号码
        /// </summary>
        public string CardID { get => cardID; set => cardID = value; }
        /// <summary>
        /// 律师号码
        /// </summary>
        public string LawyerCard { get => lawyerCard; set => lawyerCard = value; }
        /// <summary>
        /// 采访事由
        /// </summary>
        public string Purpose { get => purpose; set => purpose = value; }
        /// <summary>
        /// 手机号码
        /// </summary>
        public string Mobile { get => mobile; set => mobile = value; }
        /// <summary>
        /// 1存入2取出
        /// </summary>
        public string OperType { get => operType; set => operType = value; }
        #endregion
    }

 

posted on 2019-06-13 18:07  冰魂雪魄  阅读(2577)  评论(0编辑  收藏  举报

WPF框架交流群:C#.net. WPF.core 技术交流�      C#WPF技术交流群:C#.net. WPF.core 技术交流�     WPF技术大牛交流群:C#.net. WPF.core 技术交流�