简体   繁体   English

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

[英]C# serialize complex object of class to json

I want to convert new object of following class to json string. 我想将以下类的新对象转换为json字符串。 for this i use either JavaScriptSerializer and Newtonsoft library. 为此,我使用JavaScriptSerializer和Newtonsoft库。 but the out put for both of them is empty brackets ( {[],[]} )! 但两者的输出都是空括号({[],[]})!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace My_Entity
{
    using My_Entity.Interfaces;
    using My_Entity.Abstracts;

    public class tbl_CategoryEntity : Entity<tbl_CategoryEntity>, Itbl_Category
    {
        private Int32? _CategoryID;
        private String _CategoryName;
        private Int32? _TypeID;
        private Boolean? _IsDel;
        private static readonly string _IdentityField = "CategoryID";
        private static readonly SqlDbType _IdentitySqlDbType = SqlDbType.Int;
        private readonly Dictionary<string, SqlDbType> _FieldsSqlDbType;

        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 tbl_CategoryEntity()
        {
            _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);
        }
    }
}

tbl_CategoryEntity a = new tbl_CategoryEntity()
{
    CategoryID = 12,
    CategoryName = "hi"
};
string json = new JavaScriptSerializer().Serialize(a);

how can i fix it? 我该怎么办呢?

the base class (Entity) is: 基类(实体)是:

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);
    }
}

Okay, I think the problem is that you have mixed together the Entity class with the collection of entities class. 好吧,我认为问题在于你将Entity类与实体类的集合混合在一起。 I tried to separate the collection from the entity. 我试图将集合与实体分开。 Take a look at this code and see if it helps you out. 看看这段代码,看看它是否有助于你解决问题。

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);
        }
    }
}

Notice that the class for your entity 'tbl_CategoryEntity represents a single record from the database. But, the class 'tbl_CategoryEntityCollection 请注意,实体'tbl_CategoryEntity的类represents a single record from the database. But, the class 'tbl_CategoryEntityCollection represents a single record from the database. But, the class 'tbl_CategoryEntityCollection is used to represent a collection that can hold those records. represents a single record from the database. But, the class 'tbl_CategoryEntityCollection用于表示可以保存这些记录的集合。

Hope that points you in the right direction! 希望指出你正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM