C# 反序列化json问题-继承的linq2SQL对象
我在我的web应用程序中使用了Linq to SQL对象。我的基类和继承类如下所示:C# 反序列化json问题-继承的linq2SQL对象,c#,asp.net,json.net,deserialization,anonymous-methods,C#,Asp.net,Json.net,Deserialization,Anonymous Methods,我在我的web应用程序中使用了Linq to SQL对象。我的基类和继承类如下所示: //Base Class: this will define the attributes that is auto-generated //when using Linq-2-SQL ORM. Note this class is a partial class [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Categories")] [
//Base Class: this will define the attributes that is auto-generated
//when using Linq-2-SQL ORM. Note this class is a partial class
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Categories")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class Category : INotifyPropertyChanging, INotifyPropertyChanged
//Inherited Class:
[Serializable]
public class CategoryEntity : Category
{
private int _ActiveAdsCount;
public int ActiveAdsCount
{
get
{
return _ActiveAdsCount;
}
set
{
_ActiveAdsCount = value;
}
}
public int DisplaySequence { get; set; }
}
[DataContract]
public class CategoryEntity : Category
{
[DataMember]
public int ActiveAdsCount { get; set; }
[DataMember]
public int DisplaySequence { get; set; }
[DataMember]
public IList<CategoryEntity> SubCategories { get; set; }
[DataMember]
public IList<BasicCategoryInfo> SubCategoriesBasicInfoList { get; set; }
[DataMember]
public string ParentCategoryNameEn { get; set; }
[DataMember]
public int CityID { get; set; }
}
序列化时,Json输出为(注意ActiveAdsCount和DisplaySequence值):
[{“ActiveAdsCount”:3429,“DisplaySequence”:99,“CategoryID”:636,“ParentCategoryID”:635,“CategoryName”:“propForRent”,“CategoryImageFN”:null}]
当我调用反序列化对象方法时
result = JsonConvert.DeserializeObject<T>(responseText);
result=JsonConvert.DeserializeObject(responseText);
其中T是列表
结果:显示“ActiveAdsCount”和“DisplaySequence”的值为0,而json显示来自数据库的正确信息。因此,问题在于反序列化
我使用的是.Net 4.0 framework的Newtonsoft.Json.dll的4.5.1版本此外,为了序列化的目的,我已经用DataContract属性将我的CategoryEntity类及其成员标记为Datamember。我注意到Serialization属性只将实例设置为可序列化的,而不将其成员设置为可序列化的。因此,新类如下所示:
//Base Class: this will define the attributes that is auto-generated
//when using Linq-2-SQL ORM. Note this class is a partial class
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Categories")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class Category : INotifyPropertyChanging, INotifyPropertyChanged
//Inherited Class:
[Serializable]
public class CategoryEntity : Category
{
private int _ActiveAdsCount;
public int ActiveAdsCount
{
get
{
return _ActiveAdsCount;
}
set
{
_ActiveAdsCount = value;
}
}
public int DisplaySequence { get; set; }
}
[DataContract]
public class CategoryEntity : Category
{
[DataMember]
public int ActiveAdsCount { get; set; }
[DataMember]
public int DisplaySequence { get; set; }
[DataMember]
public IList<CategoryEntity> SubCategories { get; set; }
[DataMember]
public IList<BasicCategoryInfo> SubCategoriesBasicInfoList { get; set; }
[DataMember]
public string ParentCategoryNameEn { get; set; }
[DataMember]
public int CityID { get; set; }
}
[DataContract]
公共类类别实体:类别
{
[数据成员]
public int activeadscont{get;set;}
[数据成员]
公共int显示序列{get;set;}
[数据成员]
公共IList子类别{get;set;}
[数据成员]
公共IList子类别BASICINFOLIST{get;set;}
[数据成员]
公共字符串ParentCategoryNameEn{get;set;}
[数据成员]
public int CityID{get;set;}
}
@JasonJong非常感谢您的评论。看看这里发布的一个类似问题:@JasonJong感谢您的链接。我现在就试试!是的,它就像一个符咒:)@JasonJong非常感谢,伙计,上帝保佑