C# 仅使用所需的属性序列化模型
我的MVC4应用程序中有两个模型定义如下:C# 仅使用所需的属性序列化模型,c#,serialization,C#,Serialization,我的MVC4应用程序中有两个模型定义如下: public class Attendee { public string ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string DOB { get; set; } public string Address1 { get; set; } publi
public class Attendee
{
public string ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DOB { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Employer { get; set; }
public Info _Info { get; set; }
}
public class Info
{
public string License { get; set; }
public string State { get; set; }
public string Type { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Declined { get; set; }
}
在转换为JSON之前,我还需要能够从Attendee对象中完全删除信息
或者我应该创建另一个没有它的模型,并以某种方式将值从一个模型传递到另一个模型
最简单的方法是什么?幸运的是,您的模型中没有循环引用,因此您可以将
[JsonIgnore]
放在任何不需要json格式的属性上:
[JsonIgnore]
public Info _Info { get; set; }
听起来你真正的问题是:如何只序列化我想要的属性。这基本上是你的问题吗?听起来是正确的问题,但我正在构建一个更深入的模型,其中包含一系列Attendee对象,我需要从每个对象中删除信息。信息中包含的数据将发送到与会者中的控制器,因此我需要在将信息数据移动到更大型号的另一部分后将其删除。请注意如何回答问题。很有可能走错了路,创造了一个新的环境。有一些方法可以序列化对象,而不需要序列化对象的特定属性。因此,删除与会者的信息根本不是问题。我使用的是Newtonsoft JSON类。这对他们有用吗?是特定于Newtonsoft的。我正在使用Newtonsoft,但它说,
[JsonIgnoreAttribute]
找不到我的坏。。。需要使用Newtonsoft.Json
@Cuong请你能帮我解决这个问题吗