- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
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));
时,我收到以下错误:
A circular reference was detected while serializing an object of type 'System.Data.Metadata.Edm.AssociationType
我想要的是一个表示完整 as_system 对象的 json 对象,因此我不必手动映射每个属性。有什么办法可以解决这个问题吗?谢谢!
最佳答案
如果您想将 Entity Framework 对象序列化为 JSON,您可以使用来自 http://www.newtonsoft.com 的 JSON.NET .为此,从 nuget 安装 JSON.NET 并使用以下代码示例:
return Newtonsoft.Json.JsonConvert.SerializeObject(results, Formatting.Indented,
new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
ReferenceLoopHandling.Ignore 可以防止循环引用错误。
关于c# - 将 Entity Framework 对象序列化为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7790890/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!