将实体框架对象序列化为 JSON
public class GenericHandler : IHttpHandler
{
public class ASSystem
{
public string SID { get; set; }
public string Description { get; set; }
public string SystemName { get; set; }
}
public class ErrorObj
{
public string ErrorMessage { get; set; }
}
public void ProcessRequest(HttpContext context)
{
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
string query = HttpContext.Current.Request.QueryString["SID"];
SOFAEntities ctx = new SOFAEntities();
JavaScriptSerializer serializer = new JavaScriptSerializer();
try
{
AS_SYSTEM system = ctx.AS_SYSTEM.Where(s => s.SYSTEM_ID == query).First() as AS_SYSTEM;
if (system != null)
{
ASSystem sys = new ASSystem() { SID = system.SYSTEM_ID, Description = system.DESCRIPTION, SystemName = system.SYSTEM_NAME };
HttpContext.Current.Response.Write(serializer.Serialize(sys));
}
}
catch (Exception e)
{
HttpContext.Current.Response.Write(serializer.Serialize(new ErrorObj() { ErrorMessage = e.Message }));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
这有效,但是当我尝试使用 HttpContext.Current.Response.Write(serializer.Serialize(system));
我收到以下错误:
序列化类型的对象时检测到循环引用 'System.Data.Metadata.Edm.AssociationType
我想要的是一个表示完整 as_system 对象的 json 对象,因此我不必手动映射每个属性。有什么办法可以解决这个问题吗?谢谢!
public class GenericHandler : IHttpHandler
{
public class ASSystem
{
public string SID { get; set; }
public string Description { get; set; }
public string SystemName { get; set; }
}
public class ErrorObj
{
public string ErrorMessage { get; set; }
}
public void ProcessRequest(HttpContext context)
{
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
string query = HttpContext.Current.Request.QueryString["SID"];
SOFAEntities ctx = new SOFAEntities();
JavaScriptSerializer serializer = new JavaScriptSerializer();
try
{
AS_SYSTEM system = ctx.AS_SYSTEM.Where(s => s.SYSTEM_ID == query).First() as AS_SYSTEM;
if (system != null)
{
ASSystem sys = new ASSystem() { SID = system.SYSTEM_ID, Description = system.DESCRIPTION, SystemName = system.SYSTEM_NAME };
HttpContext.Current.Response.Write(serializer.Serialize(sys));
}
}
catch (Exception e)
{
HttpContext.Current.Response.Write(serializer.Serialize(new ErrorObj() { ErrorMessage = e.Message }));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
This works, but when i try with HttpContext.Current.Response.Write(serializer.Serialize(system));
i get the following error:
A circular reference was detected while serializing an object of type
'System.Data.Metadata.Edm.AssociationType
What i wanted was a json object representating the complete as_system object, so i dont have to map each property manually. Is there any way to solve this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想将实体框架对象序列化为 JSON,您可以使用 http://www.newtonsoft.com。为此,请从 nuget 安装 JSON.NET 并使用以下代码示例:
ReferenceLoopHandling.Ignore 可以防止循环引用错误。
If you want Serialize Entity Framework Objects into JSON, You can use JSON.NET from http://www.newtonsoft.com. to do this, install JSON.NET from nuget and use following code sample:
ReferenceLoopHandling.Ignore can prevent circular reference error.
听起来 EF 并没有为您提供一个
ASSystem
,而是提供了一些带有 EF goo 的微妙动态子类。如果这是正确的,我认为这里要做的最简单的事情就是使用类似 AutoMapper< /a> 获取非 EF 副本(进入new ASSystem()
实例,不受 EF 影响)。但是,有一些替代方案:ASSystem
标记为sealed
,从而消除 EF 注入自身的能力It sounds like EF is not giving you a
ASSystem
, but rather some subtle dynamic subclass of that with some EF goo. If that is correct, I would argue the simplest thing to do here is to use something like AutoMapper to get a non-EF copy (into anew ASSystem()
instance, untouched by EF). However, a few alternatives:ASSystem
assealed
, taking away EF's ability to inject itself您可以创建一个 POCO 对象,该对象可以包含您的数据并且可以序列化。例如,定义:
在您的代码中使用以下语句:
现在您可以像示例中那样序列化
sys
。You can create a POCO object that can contains your data and can be serialized. For example define:
in your code use this statement:
Now you can serialize
sys
as in your example.不确定这是否有帮助,但尝试使用 DataContractJsonSerializer 而不是 JavaScriptSerializer。据我所知,DataContractJsonSerializer 优于 JavaScriptSerializer。
Not sure if this will help, but try to use DataContractJsonSerializer instead of JavaScriptSerializer. As far as I know DataContractJsonSerializer is prefered over JavaScriptSerializer.
试试这个;它对我有用。
要返回 JSON 数据 [在 EF 中]:
System.Runtime.Serialization
添加到项目中。您还可以在 http://jsonlint.com/ 检查 JSON 字符串是否有效。
Try this; it works for me.
To return JSON data [in EF]:
System.Runtime.Serialization
to the project.You can also check whether JSON string is valid at http://jsonlint.com/.