【发布时间】:2017-11-25 18:31:44
【问题描述】:
我想将以下类的新对象转换为 json 字符串。为此,我使用 JavaScriptSerializer 和 Newtonsoft 库。但是它们的输出都是空括号( {[],[]} )!
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);
我该如何解决?
【问题讨论】:
-
我们可能需要查看您派生的基类。这个基础是否有可能使用字典来存储实体值?如果是这样,这可以解释为什么您在序列化数据中得到一个数组而不是您期望的属性。
-
因为我被列出了该对象的列表,所以返回了数组。当我在对象上进行序列化时,输出为空
-
你可能一开始就不想直接序列化实体类。
标签: c# json object serialization