我想將以下類的新對象轉換爲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);
我該如何解決它?
我們可能需要查看您從中派生出的基類。這個基地可能使用字典來存儲實體值嗎?如果是這樣,那可以解釋爲什麼你要在序列化數據中獲取數組,而不是你期望的屬性。 –
因爲我曾經制作過該對象的列表,數組返回。當我甚至在對象上序列化輸出爲空時 – Masoud
你可能不想直接序列化實體類。 – Casey