C#将类的复杂对象序列化为json

C#将类的复杂对象序列化为json,c#,json,object,serialization,C#,Json,Object,Serialization,我想将以下类的新对象转换为json字符串。为此,我使用JavaScriptSerializer和Newtonsoft库。但这两个词的输出都是空括号({[],[]}) 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用系统数据; 名称空间My_实体 { 使用My_Entity.Interfaces; 使用My_Entity.Abstracts; 公共类tbl_类别实体:实体、Itbl_类别 { 私有Int32?

我想将以下类的新对象转换为json字符串。为此,我使用JavaScriptSerializer和Newtonsoft库。但这两个词的输出都是空括号({[],[]})

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
名称空间My_实体
{
使用My_Entity.Interfaces;
使用My_Entity.Abstracts;
公共类tbl_类别实体:实体、Itbl_类别
{
私有Int32?_类别ID;
私有字符串_CategoryName;
私有Int32?\u类型ID;
私有布尔?_-IsDel;
私有静态只读字符串_IdentityField=“CategoryID”;
私有静态只读SqlDbType _IdentitySqlDbType=SqlDbType.Int;
专用只读字典_FieldsSqlDbType;
公共Int32?类别ID
{
获取{return\u CategoryID;}
设置{u CategoryID=value;}
}
公共字符串类别名称
{
获取{return\u CategoryName;}
设置{u CategoryName=value;}
}
公共Int32?类型ID
{
获取{return\u TypeID;}
设置{u TypeID=value;}
}
公共布尔值
{
获取{return\u IsDel;}
设置{u IsDel=value;}
}
公共tbl_类别实体()
{
_FieldsSqlDbType=新字典()
{
{“CategoryID”,SqlDbType.Int},
{“CategoryName”,SqlDbType.NVarChar},
{“TypeID”,SqlDbType.Int},
{“IsDel”,SqlDbType.Bit}
}.Union(base.\u FilterFieldsSqlDbType).ToDictionary(k=>k.Key,v=>v.Value);
}
公共静态字符串GetIdentityField()
{
返回_IdentityField;
}
公共静态SqlDbType GetIdentitySqlDbType()
{
返回_IdentitySqlDbType;
}
公共重写SqlDbType GetSqlDbType(字符串属性名称)
{
返回_FieldsSqlDbType[PropertyName];
}
公共覆盖布尔IsIdentity(字符串属性名称)
{
返回PropertyName.Equals(_IdentityField);
}
}
}
tbl_类别实体a=新tbl_类别实体()
{
类别ID=12,
CategoryName=“嗨”
};
string json=new JavaScriptSerializer().Serialize(a);
如何修复它?

基类(实体)是:

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Data.Sql;
名称空间My_Entity.Abstracts
{
使用My_Entity.Interfaces;
公共抽象类实体:列表,其中T:new()
{
私有字符串_OrderColumn;
私有字符串_Order=“asc”;
私有Int32?\u页面索引=1;
私有Int32?_RowsPage=10;
受保护的只读字典\u filterFieldSqlDbType;
公共字符串OrderColumn
{
获取{return\u OrderColumn;}
设置{u OrderColumn=value;}
}
公共字符串秩序
{
获取{return\u Order;}
设置{u顺序=值;}
}
公共Int32?页面索引
{
获取{return\u PageIndex;}
设置{u PageIndex=value;}
}
公共Int32?RowsPage
{
获取{return\u RowsPage;}
设置{u RowsPage=value;}
}
公共实体()
{
_FilterFieldSqlDbType=新字典()
{ 
{“OrderColumn”,SqlDbType.VarChar},
{“Order”,SqlDbType.VarChar},
{“PageIndex”,SqlDbType.Int},
{“RowsPage”,SqlDbType.Int},
};
}
公共抽象SqlDbType GetSqlDbType(stringpropertyname);
公共抽象属性(字符串属性名称);
}
}
基类(实体)是:

使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Data.Sql;
名称空间My_Entity.Abstracts
{
使用My_Entity.Interfaces;
公共抽象类实体:列表,其中T:new()
{
私有字符串_OrderColumn;
私有字符串_Order=“asc”;
私有Int32?\u页面索引=1;
私有Int32?_RowsPage=10;
受保护的只读字典\u filterFieldSqlDbType;
公共字符串OrderColumn
{
获取{return\u OrderColumn;}
设置{u OrderColumn=value;}
}
公共字符串秩序
{
获取{return\u Order;}
设置{u顺序=值;}
}
公共Int32?页面索引
{
获取{return\u PageIndex;}
设置{u PageIndex=value;}
}
公共Int32?RowsPage
{
获取{return\u RowsPage;}
设置{u RowsPage=value;}
}
公共实体()
{
_FilterFieldSqlDbType=新字典()
{ 
{“OrderColumn”,SqlDbType.VarChar},
{“Order”,SqlDbType.VarChar},
{“PageIndex”,SqlDbType.Int},
{“RowsPage”,SqlDbType.Int},
};
}
公共抽象SqlDbType GetSqlDbType(stringpropertyname);
公共抽象属性(字符串属性名称);
}
}

好的,我认为问题在于您将实体类与实体集合类混合在一起了。我试图将集合与实体分开。请看一下这段代码,看看它是否对您有所帮助

namespace ConsoleApp1
{
    using System;
    using System.Linq;
    using Newtonsoft.Json;
    using System.Data;
    using System.Collections.Generic;

    class Program
    {
        static void Main(string[] args)
        {
            var t = new tbl_CategoryEntity
            {
                CategoryID = 12,
                CategoryName = "Hi"
            };
            var collection = new tbl_CategoryEntityCollection();
            collection.Add(t);

            var entityJson = JsonConvert.SerializeObject(t);
            var collectionJson = JsonConvert.SerializeObject(collection);
            Console.WriteLine("Entity = \n" + entityJson);
            Console.WriteLine("Collection = \n" + collectionJson);

            Console.ReadKey();
        }
    }

    public class tbl_CategoryEntity
    {
        private Int32? _CategoryID;
        private String _CategoryName;
        private Int32? _TypeID;
        private Boolean? _IsDel;

        public tbl_CategoryEntity() { }

        public Int32? CategoryID
        {
            get { return _CategoryID; }
            set { _CategoryID = value; }
        }

        public String CategoryName
        {
            get { return _CategoryName; }
            set { _CategoryName = value; }
        }

        public Int32? TypeID
        {
            get { return _TypeID; }
            set { _TypeID = value; }
        }

        public Boolean? IsDel
        {
            get { return _IsDel; }
            set { _IsDel = value; }
        }
    }

    public abstract class EntityCollection<T> : List<T> where T : new()
    {
        private String _OrderColumn;
        private String _Order = "asc";
        private Int32? _PageIndex = 1;
        private Int32? _RowsPage = 10;
        protected readonly Dictionary<string, SqlDbType> _FilterFieldsSqlDbType;

        public String OrderColumn
        {
            get { return _OrderColumn; }
            set { _OrderColumn = value; }
        }
        public String Order
        {
            get { return _Order; }
            set { _Order = value; }
        }
        public Int32? PageIndex
        {
            get { return _PageIndex; }
            set { _PageIndex = value; }
        }
        public Int32? RowsPage
        {
            get { return _RowsPage; }
            set { _RowsPage = value; }
        }

        protected EntityCollection()
        {
            _FilterFieldsSqlDbType = new Dictionary<string, SqlDbType>()
            {
                { "OrderColumn", SqlDbType.VarChar },
                { "Order", SqlDbType.VarChar },
                { "PageIndex", SqlDbType.Int },
                { "RowsPage", SqlDbType.Int },
            };
        }

        public abstract SqlDbType GetSqlDbType(string PropertyName);

        public abstract bool IsIdentity(string PropertyName);
    }

    public class tbl_CategoryEntityCollection : EntityCollection<tbl_CategoryEntity>
    {
        private static readonly string _IdentityField = "CategoryID";
        private static readonly SqlDbType _IdentitySqlDbType = SqlDbType.Int;
        private readonly Dictionary<string, SqlDbType> _FieldsSqlDbType;

        public tbl_CategoryEntityCollection() : base()
        {
            _FieldsSqlDbType = new Dictionary<string, SqlDbType>()
        {
            { "CategoryID", SqlDbType.Int },
            { "CategoryName", SqlDbType.NVarChar },
            { "TypeID", SqlDbType.Int },
            { "IsDel", SqlDbType.Bit }
        }
            .Union(base._FilterFieldsSqlDbType).ToDictionary(k => k.Key, v => v.Value);
        }

        public static string GetIdentityField()
        {
            return _IdentityField;
        }

        public static SqlDbType GetIdentitySqlDbType()
        {
            return _IdentitySqlDbType;
        }

        public override SqlDbType GetSqlDbType(string PropertyName)
        {
            return _FieldsSqlDbType[PropertyName];
        }

        public override bool IsIdentity(string PropertyName)
        {
            return PropertyName.Equals(_IdentityField);
        }
    }
}
名称空间控制台EAPP1
{
使用制度;
使用System.Linq;
使用Newtonsoft.Json;
使用系统数据;
使用System.Collections.Generic;
班级计划
{
静态void Main(字符串[]参数)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;

namespace My_Entity.Abstracts
{
    using My_Entity.Interfaces;

    public abstract class Entity<T> : List<T>, IEntity where T : new()
    {
        private String _OrderColumn;
        private String _Order = "asc";
        private Int32? _PageIndex = 1;
        private Int32? _RowsPage = 10;
        protected readonly Dictionary<string, SqlDbType> _FilterFieldsSqlDbType;

        public String OrderColumn
        {
            get { return _OrderColumn; }
            set { _OrderColumn = value; }
        }
        public String Order
        {
            get { return _Order; }
            set { _Order = value; }
        }
        public Int32? PageIndex
        {
            get { return _PageIndex; }
            set { _PageIndex = value; }
        }
        public Int32? RowsPage
        {
            get { return _RowsPage; }
            set { _RowsPage = value; }
        }

        public Entity()
        {
            _FilterFieldsSqlDbType = new Dictionary<string, SqlDbType>()
            { 
                { "OrderColumn", SqlDbType.VarChar },
                { "Order", SqlDbType.VarChar },
                { "PageIndex", SqlDbType.Int },
                { "RowsPage", SqlDbType.Int },
            };
        }

        public abstract SqlDbType GetSqlDbType(string PropertyName);

        public abstract bool IsIdentity(string PropertyName);
    }
}
namespace ConsoleApp1
{
    using System;
    using System.Linq;
    using Newtonsoft.Json;
    using System.Data;
    using System.Collections.Generic;

    class Program
    {
        static void Main(string[] args)
        {
            var t = new tbl_CategoryEntity
            {
                CategoryID = 12,
                CategoryName = "Hi"
            };
            var collection = new tbl_CategoryEntityCollection();
            collection.Add(t);

            var entityJson = JsonConvert.SerializeObject(t);
            var collectionJson = JsonConvert.SerializeObject(collection);
            Console.WriteLine("Entity = \n" + entityJson);
            Console.WriteLine("Collection = \n" + collectionJson);

            Console.ReadKey();
        }
    }

    public class tbl_CategoryEntity
    {
        private Int32? _CategoryID;
        private String _CategoryName;
        private Int32? _TypeID;
        private Boolean? _IsDel;

        public tbl_CategoryEntity() { }

        public Int32? CategoryID
        {
            get { return _CategoryID; }
            set { _CategoryID = value; }
        }

        public String CategoryName
        {
            get { return _CategoryName; }
            set { _CategoryName = value; }
        }

        public Int32? TypeID
        {
            get { return _TypeID; }
            set { _TypeID = value; }
        }

        public Boolean? IsDel
        {
            get { return _IsDel; }
            set { _IsDel = value; }
        }
    }

    public abstract class EntityCollection<T> : List<T> where T : new()
    {
        private String _OrderColumn;
        private String _Order = "asc";
        private Int32? _PageIndex = 1;
        private Int32? _RowsPage = 10;
        protected readonly Dictionary<string, SqlDbType> _FilterFieldsSqlDbType;

        public String OrderColumn
        {
            get { return _OrderColumn; }
            set { _OrderColumn = value; }
        }
        public String Order
        {
            get { return _Order; }
            set { _Order = value; }
        }
        public Int32? PageIndex
        {
            get { return _PageIndex; }
            set { _PageIndex = value; }
        }
        public Int32? RowsPage
        {
            get { return _RowsPage; }
            set { _RowsPage = value; }
        }

        protected EntityCollection()
        {
            _FilterFieldsSqlDbType = new Dictionary<string, SqlDbType>()
            {
                { "OrderColumn", SqlDbType.VarChar },
                { "Order", SqlDbType.VarChar },
                { "PageIndex", SqlDbType.Int },
                { "RowsPage", SqlDbType.Int },
            };
        }

        public abstract SqlDbType GetSqlDbType(string PropertyName);

        public abstract bool IsIdentity(string PropertyName);
    }

    public class tbl_CategoryEntityCollection : EntityCollection<tbl_CategoryEntity>
    {
        private static readonly string _IdentityField = "CategoryID";
        private static readonly SqlDbType _IdentitySqlDbType = SqlDbType.Int;
        private readonly Dictionary<string, SqlDbType> _FieldsSqlDbType;

        public tbl_CategoryEntityCollection() : base()
        {
            _FieldsSqlDbType = new Dictionary<string, SqlDbType>()
        {
            { "CategoryID", SqlDbType.Int },
            { "CategoryName", SqlDbType.NVarChar },
            { "TypeID", SqlDbType.Int },
            { "IsDel", SqlDbType.Bit }
        }
            .Union(base._FilterFieldsSqlDbType).ToDictionary(k => k.Key, v => v.Value);
        }

        public static string GetIdentityField()
        {
            return _IdentityField;
        }

        public static SqlDbType GetIdentitySqlDbType()
        {
            return _IdentitySqlDbType;
        }

        public override SqlDbType GetSqlDbType(string PropertyName)
        {
            return _FieldsSqlDbType[PropertyName];
        }

        public override bool IsIdentity(string PropertyName)
        {
            return PropertyName.Equals(_IdentityField);
        }
    }
}