1. 程式人生 > >netcore之json序列化

netcore之json序列化

首先定義一個類

    [DataContract]
    class RecordFile
    {
        [DataMember]
        public string StartTime
        {
            get;
            set;
        }
        [DataMember]
        public string EndTime
        {
            get;
            set;
        }
        [DataMember]
        public string Size
        {
            get;
            set;
        }
    }

序列化成字串

                  MemoryStream stream1 = new MemoryStream();
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RecordFileList));
                    ser.WriteObject(stream1, recordFileList);
                    LogHelper.Info(Encoding.Default.GetString(stream1.ToArray()));