次のクラスの新しいオブジェクトをjson文字列に変換したいと思います。私はJavaScriptSerializerとNewtonsoftのどちらかのライブラリを使います。両方の出力は空の括弧({[]、[]})です!クラスの複雑なオブジェクトを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