2017-06-22 39 views
1

我想將以下類的新對象轉換爲json字符串。爲此我使用JavaScriptSerializer和Newtonsoft庫。但是他們都是空括號({[],[]})!C#將類的複雜對象序列化爲json

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

我該如何解決它?

+0

我們可能需要查看您從中派生出的基類。這個基地可能使用字典來存儲實體值嗎?如果是這樣,那可以解釋爲什麼你要在序列化數據中獲取數組,而不是你期望的屬性。 –

+0

因爲我曾經制作過該對象的列表,數組返回。當我甚至在對象上序列化輸出爲空時 – Masoud

+0

你可能不想直接序列化實體類。 – Casey

回答

0

基類(實體)是:

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

好吧,我認爲這個問題是你已經混在一起實體類實體類的集合。我試圖將收集與實體分開。看看這段代碼,看看它是否可以幫助你。

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

注意,對你的實體「tbl_CategoryEntity represents a single record from the database. But, the class 'tbl_CategoryEntityCollection類是用來表示一個集合,可以持有這些記錄。

希望你指出正確的方向!