相关疑难解决方法(0)

Json.Net中的PreserveReferencesHandling和ReferenceLoopHandling有什么区别?

我正在查看一个具有以下编码的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的新手,所以有人可以通过简单的方式向我解释这些差异是什么以及为什么我可能需要使用一个而不是另一个.

c# asp.net json json.net asp.net-web-api

59
推荐指数
1
解决办法
2万
查看次数

使用Web API和JSON.NET序列化对象时,防止$ id/$ ref

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)

有任何想法吗?

c# json.net asp.net-mvc-4 asp.net-web-api

15
推荐指数
3
解决办法
1万
查看次数

为什么Web Api返回空的json结果为"$ ref ="?

我在EF5中禁用了延迟加载和代理创建,然后在Web Api中返回任何结果.一切都很好.但是,当我开始Include()其他实体进行急切提取时,一些JSON对象如下所示:

{
$ref: "14"
},
.. correct objects ..
{
$ref: "6"
},
..
Run Code Online (Sandbox Code Playgroud)

这是什么"$ref"包含在结果列表中?

.net json asp.net-mvc-4 asp.net-web-api

6
推荐指数
1
解决办法
5247
查看次数

标签 统计

asp.net-web-api ×3

asp.net-mvc-4 ×2

c# ×2

json ×2

json.net ×2

.net ×1

asp.net ×1