我正在查看一个具有以下编码的WebAPI应用程序示例:
json.SerializerSettings.PreserveReferencesHandling
= Newtonsoft.Json.PreserveReferencesHandling.Objects;
Run Code Online (Sandbox Code Playgroud)
和另一个有这个编码:
json.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Run Code Online (Sandbox Code Playgroud)
既没有解释为什么选择每一个.我是WebAPI的新手,所以有人可以通过简单的方式向我解释这些差异是什么以及为什么我可能需要使用一个而不是另一个.
Newtonsoft.Json.PreserveReferencesHandling.Objects在序列化对象时,我似乎无法阻止Web API/JSON.NET的使用.换句话说,尽管使用了以下设置,但$ id/$ ref总是在序列化对象中使用:
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start () {
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
public static class WebApiConfig {
public static void Register (HttpConfiguration config) {
JsonMediaTypeFormatter jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().Single();
jsonFormatter.UseDataContractJsonSerializer = false;
jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在EF5中禁用了延迟加载和代理创建,然后在Web Api中返回任何结果.一切都很好.但是,当我开始Include()其他实体进行急切提取时,一些JSON对象如下所示:
{
$ref: "14"
},
.. correct objects ..
{
$ref: "6"
},
..
Run Code Online (Sandbox Code Playgroud)
这是什么"$ref"包含在结果列表中?