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

2024-02-21

我想将以下类的新对象转换为 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);

我该如何修复它?


基类(实体)是:

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);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 将类的复杂对象序列化为json 的相关文章

随机推荐

  • 保存时,将添加的文本视图位置保留在图像视图上

    有一个主要活动有两个按钮 一个是 添加文本 另一个是 保存图像 这是主要布局的一部分
  • 在 iOS 6 上使用 AFNetworking 下载文件

    我最近更新到 AFNetworking 2 0 文档说兼容iOS6 0 当我尝试实现下载方法 图像和视频 时 我正在构建一个 iOS 6 0 应用程序 示例使用 AFURLSessionManager manager AFURLSessio
  • 始终生成为 h2 DDL 中的身份

    在我们的项目中 我们使用 postgres 数据库作为开发到生产环境 对于 junit 测试 我们在内存数据库中使用 H2 想知道我们是否可以使用 GENERATE ALWAYS AS IDENTITY 创建 DDL 以在 H2 数据库中插
  • nohup 作为后台任务不返回提示

    我试图在后台运行一个长时间运行的任务 而无需登录并让终端返回提示 但是当我这样做时 该任务似乎进入后台 但我的提示不可用 除非我按 Control C 我想运行任务然后得到提示 staging php pos web nohup php t
  • 如何解析 Scala 中的命令行参数? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 在 Scala 中解析命令行参数的好方法是什么 Related 如何解析 Java 中的命令行参数 https stackoverflow co
  • android jdbc odbc 连接

    我想将 odbc 连接连接到我的 Android 应用程序 我的数据库是oracle 10g 在我的代码中 我的表名称是 world 编译我的程序并关闭模拟器在 oracle 数据库中打开的表后 无法存储值 我在 cmp 中编译普通 jav
  • 使用 pandas.cut 和 IntervalIndex 后如何重命名类别?

    我使用离散化数据框中的一列pandas cut与创建的垃圾箱IntervalIndex from tuples 剪切按预期工作 但是类别显示为我在IntervalIndex 有什么方法可以将类别重命名为不同的标签 例如 小号中号大号 Exa
  • 如何使用 CSS 定位文本输入字段?

    假设您有以下 HTML
  • 使用JAVA从jar文件中读取MANIFEST.MF文件

    有什么方法可以读取 jar 文件的内容吗 就像我想读取清单文件以便找到 jar 文件的创建者和版本一样 有什么办法可以达到同样的目的 下一个代码应该有帮助 JarInputStream jarStream new JarInputStrea
  • 正则表达式匹配两个不相等的数字

    我想知道如何匹配两个不同的数字 所以 匹配 12 就可以了 但不能匹配 11 到目前为止我所拥有的是 我必须匹配像 P12 这样的字符串 我已经用这个正则表达式完成了 P 1 6 1 1 6 1 但现在我的问题是如何仅匹配 P12 或 P3
  • 应用程序之间的对象共享?

    假设我有一个每秒更新 1000 多次的大型数据数组 另一个应用程序想要在短时间内访问和读取数组 两个应用程序都在同一台机器上 我曾尝试使用 WCF 进行进程间通信 但每秒序列化和发送整个数组 或大型对象 数千次在性能方面是不可行的 有没有办
  • 如何优化连字符

    我正在努力解决浏览器处理逐行对齐文本连字的不同方式 我的文本有以下 css 设置 text align justify webkit hyphens auto moz hyphens auto ms hyphens auto hyphens
  • 为什么Android中只有UI线程才能更新UI?

    有人可以向我解释一下为什么Android中只有UI线程可以更新UI吗 为什么其他线程不能更新 UI 文档 https developer android com guide components processes and threads
  • 如何以编程方式更新 ui-grid 中的排序指示器?

    我正在使用 ui grid v3 0 0 rc 22 2015 06 15 它被配置为使用外部排序 效果很好 现在我需要使用选择框从外部更改排序列 每次更改选择框时 它都会触发外部排序 并且网格中的数据会正确更新 它还更新 gridOpti
  • Spring Data JPA JSONB 参数化

    将此查询转换为 Spring Data Repository 的正确语法 JPA Spring Data 或 SpEL 是什么nativeQuery SELECT FROM mytable WHERE f jsonb arr lower m
  • 在 vscode 中添加特定于语言的图标

    我已经创建了一个语言扩展 语法突出显示等 现在想要将特定图标与该语言 或具有我的扩展中指定的扩展名的文件 相关联 我注意到一个 icon 财产可在package json文件 并尝试将此属性绑定到我的 svg文件 但是 虽然语法突出显示在测
  • 为什么我无法停止 RDS 实例?

    I spun up two RDS instances in the same VPC Both are postgresql While the m4 xlarge one has multi AZ enabled and is encr
  • 在配置单元中支持外部表的数组列类型的最佳方法是什么?

    所以我有制表符分隔数据的外部表 一个简单的表格如下所示 create external table if not exists categories id string tag string legid string image strin
  • 具有多个条件的聚合以及基于计数键的求和匹配

    我试图获取另一个集合中的对象数组中存在的元素的计数 例子 集合A id 1 name Sample1 id 2 name Sample 2 id 3 name Sample 3 id 4 name Sample 4 集合B id 11 it
  • C# 将类的复杂对象序列化为json

    我想将以下类的新对象转换为 json 字符串 为此 我使用 JavaScriptSerializer 和 Newtonsoft 库 但它们的输出都是空括号 using System using System Collections usin