簡體   English   中英

使用JsonConvert.SerializeObject時發生可枚舉錯誤

[英]Error for enumerable when using JsonConvert.SerializeObject

我嘗試使用json.net將對象轉換為json時出現錯誤。

錯誤:

  Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type
 'MyNamespace.Domain.Entity'.

要序列化的類:

[Serializable]
public class Business:Entity
    {
        public virtual string TemplateName { get; set; }

        public virtual CalculationBasis CalculationBasis { get; set; }

        public virtual PeriodSelectionType PeriodSelectionType { get; set; }

        public virtual DateTime PeriodEndDate { get; set; }

        public virtual IEnumerable<int> mainKeys { get; set; }
  }

序列化代碼:

JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.NullValueHandling = NullValueHandling.Ignore;
    settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
    var strJson = JsonConvert.SerializeObject( ObjectOfBusiness, settings);

反序列化代碼:

JsonConvert.DeserializeObject<Business>(ObjectOfBusiness, settings);

當我在IEnumerable<int> mainKeys有值時,我只會收到此錯誤

注意:mainKeys是一個List<int>

看起來錯誤是由於其父類“ Entity”,該類是這樣的:

  [Serializable]
    public abstract class Entity
    {
        public Entity()
        {
        }

        public Entity(int id)
        {
            this.Id = id;
        }

        public virtual int Id { get; set; }

        public override bool Equals(object obj)
        {
            Entity other = (Entity)obj;
            return this.Id == other.Id;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
    }

不知道為什么json.net試圖將IEnumerable轉換為“實體”類型(其父類)。

我無法從許多地方刪除已使用的實體(父類)。

請提出建議。

謝謝

終於找到了解決方案。 問題是由於override bool Equals(object obj)方法中的代碼不正確。 正確的代碼是:

  public override bool Equals(object obj)
        {
            if (obj is Entity)
            {
                Entity other = (Entity) obj;
                return this.Id == other.Id;
            }
            else
            {
                return false;
            }
        }

http://json.codeplex.com/workitem/16554

這並不是真正的答案-這是“效果很好”:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
public class Entity { }
public class CalculationBasis { }
public class PeriodSelectionType { }
[Serializable]
public class Business : Entity
{
    public virtual string TemplateName { get; set; }
    public virtual CalculationBasis CalculationBasis { get; set; }
    public virtual PeriodSelectionType PeriodSelectionType { get; set; }
    public virtual DateTime PeriodEndDate { get; set; }
    public virtual IEnumerable<int> mainKeys { get; set; }
}
class program
{
    static void Main()
    {
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.NullValueHandling = NullValueHandling.Ignore;
        settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;

        var ObjectOfBusiness = new Business
        {
            TemplateName = "abc",
            CalculationBasis = new CalculationBasis(),
            PeriodSelectionType = new PeriodSelectionType(),
            PeriodEndDate = new DateTime(),
            mainKeys = new int[] { 1, 2, 3, 4, 5 }
        };
        var strJson = JsonConvert.SerializeObject(ObjectOfBusiness, settings);
        //...
        var obj = JsonConvert.DeserializeObject<Business>(strJson, settings);
        // ^^^^ all good
    }
}

所以:如果您可以提出失敗的案例,或者給我們更多線索繼續下去,那將是非常好的。 有關信息,上面的JSON是:

{"TemplateName":"abc","CalculationBasis":{},"PeriodSelectionType":{},"PeriodEndDate":"\/Date(-62135596800000)\/","mainKeys":[1,2,3,4,5]}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM