【问题标题】:Error Serializing Datatable Into JSON using Javascript serializer使用 Javascript 序列化程序将数据表序列化为 JSON 时出错
【发布时间】:2015-03-06 13:39:51
【问题描述】:

我有一门课,

public class Parameters
{
   public int Id {get;set;}
   public string FilterParam {get;set;}
   public string NameParam {get;set;}
   public IList<int> CollectionsIds {get;set;}
   public DataTable ParamTable {get;set;}
}

当我尝试序列化为 JSON 时,我收到循环引用错误“序列化对象时检测到循环引用”。

我的序列化代码是:

JSONSerializer.SerializeObject<Parameters>(parameters)

private static JavaScriptSerializer _jsonSerializer = null;
private static JavaScriptSerializer JSONSerializer
{
    get
    {
        if (_jsonSerializer == null)
        {
            _jsonSerializer = new JavaScriptSerializer();
            _jsonSerializer.MaxJsonLength = 999999999;
        }
        return _jsonSerializer;
    }
}

public static string SerializeObject<T>(T obj)
{
    return JSONSerializer.Serialize(obj);
}

如果类也包含dataTable,如何将类序列化为JSON格式?

注意:如果我将 dataTable 设置为 null,它可以正常工作,但是每当我尝试填充 dataTable 并调用方法来序列化它时都会失败。

【问题讨论】:

  • 我薄的 Paramtable 具有与 Parameters 类相同的字段。看看这个stackoverflow.com/questions/17818386/…
  • 对你来说忽略DataTable就够了吗?
  • @Tolga 不,我需要将 Datatable 与 Parameters 类一起存储到 JSON。在最坏的情况下,我可能需要通过创建与 DataTable 对应的类来解决问题。但我正在尝试为现有的类结构找到解决方案。
  • 一个 V2Solutions,没有它的 DataTable 和 name 完全不同。我的意思是数据表的列名与类参数中声明的属性不同。

标签: c# json serialization


【解决方案1】:

只需添加一个字段(字典列表)即可将您的 DataTable 伪造到您的班级。并忽略DataTable 成员。然后你可以根据需要序列化这个类的一个对象。

public class Parameters
{
    public int Id {get;set;}
    public string FilterParam {get;set;}
    public string NameParam {get;set;}
    public IList<int> CollectionsIds {get;set;}
    [ScriptIgnore]
    public DataTable ParamTable {get;set;}

    public List<Dictionary<string, object>> _fakeParamTable
    {
        get
        {
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;

            foreach ( DataRow dr in ParamTable .Rows )
            {
                row = new Dictionary<string, object>();
                foreach ( DataColumn col in dt.Columns )
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return rows;
        }
    }
}

【讨论】:

    【解决方案2】:

    使用这个库:https://www.nuget.org/packages/newtonsoft.json/

    这个 JSON:

    {
     Parameters :
     {
       id: 1,
       FilterParam: "2",
       NameParam: "2",
       CollectionsIds: [1,2,3,1],
       ParamTable: {id:1,name:2,code:"3"}
     }
    }
    

    将有课程(http://json2csharp.com/):

    p

    ublic class ParamTable
    {
        public int id { get; set; }
        public int name { get; set; }
        public string code { get; set; }
    }
    
    public class Parameters
    {
        public int id { get; set; }
        public string FilterParam { get; set; }
        public string NameParam { get; set; }
        public List<int> CollectionsIds { get; set; }
        public ParamTable ParamTable { get; set; }
    }
    
    public class RootObject
    {
        public Parameters Parameters { get; set; }
    }
    

    所以你将 siple 序列化,你想要这样:

    JsonConvert.DeserializeObject<RootObject>(myJSON);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2021-09-02
    • 2015-01-11
    相关资源
    最近更新 更多