【发布时间】:2013-03-20 17:09:20
【问题描述】:
我有这样的类结构。问题是当我转移到之前序列化的 web api 控制器用户 json 时,被执行人计数列表为 0。 序列化的 Json 看起来是这样的:
{"$id":"1","UserId":1,"RealName":"vas`ka","PasswordHash":"password",
"Email":"somemail","Actionees":
{"$id":"2","$values":
[{"$id":"3","ActioneeId":1,"Status":"doing", "CloseDate":null,
"DeletedFlag":false, "CreationDate":"2013-03-11T11:48:24.08",
"Users":null, "Comments":null}, {"$id":"4","ActioneeId":2,"Status":"doing",
"CloseDate":null, "DeletedFlag":false,
"CreationDate":"2013-03-11T12:46:08.787", "Users":null,"Comments":null}
]
}
}
public class User
{
[Key]
public Int64 UserId { get; set; }
public String Email { get; set; }
public String RealName { get; set; }
public String PasswordHash { get; set; }
public virtual List<Actionee> Actionees { get; set; }
}
public class Actionee
{
[Key]
public Int64 ActioneeId { get; set; }
public String Status { get; set; }
public DateTime? CloseDate { get; set; }
public Boolean DeletedFlag { get; set; }
public DateTime? CreationDate { get; set; }
public virtual List<User> Users { get; set; }
public virtual IEnumerable<Actionee> Comments { get; set; }
}
我将此字符串添加到 application_start 中:
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
var dcs = new DataContractSerializer(typeof(User), null, int.MaxValue, false, true, null);
xml.SetSerializer<User>(dcs);
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.All;
And I tried to deserialized in this way: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
【问题讨论】:
-
你使用的是 json.net?
-
没有。我尝试在 WebApi 中使用 newtonsoft 执行此操作,但它可以序列化,但是当我尝试绑定时,没有 Actionees 列表。
-
能否请您添加一些您的序列化和反序列化代码
-
好的,您可以通过添加此代码(序列化和反序列化方法)来编辑您的问题吗?我可以测试一下
-
最后一个问题:字符串结果是实体框架对象序列化的结果?
标签: json entity-framework asp.net-web-api model-binding