C# 如何在 MVC 4 Web API 中为 Json.NET 设置自定义 JsonSerializerSettings?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13274625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:01:24  来源:igfitidea点击:

How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?

c#asp.net-web-apijson.net

提问by Richard Neil Ilagan

I understand that ASP.NET Web API natively uses Json.NET for (de)serializing objects, but is there a way to specify a JsonSerializerSettingsobject that you want for it to use?

我知道 ASP.NET Web API 本机使用 Json.NET 来(反)序列化对象,但是有没有办法指定JsonSerializerSettings您希望它使用的对象?

For example, what if I wanted to include typeinformation into the serialized JSON string? Normally I'd inject settings into the .Serialize()call, but Web API does that silently. I can't find a way to inject settings manually.

例如,如果我想type在序列化的 JSON 字符串中包含信息怎么办?通常我会在.Serialize()调用中注入设置,但 Web API 会默默地做到这一点。我找不到手动注入设置的方法。

采纳答案by carlosfigueira

You can customize the JsonSerializerSettingsby using the Formatters.JsonFormatter.SerializerSettingsproperty in the HttpConfigurationobject.

您可以JsonSerializerSettings使用对象中的Formatters.JsonFormatter.SerializerSettings属性自定义HttpConfiguration

For example, you could do that in the Application_Start() method:

例如,您可以在 Application_Start() 方法中执行此操作:

protected void Application_Start()
{
    HttpConfiguration config = GlobalConfiguration.Configuration;
    config.Formatters.JsonFormatter.SerializerSettings.Formatting =
        Newtonsoft.Json.Formatting.Indented;
}

回答by smockle

You can specify JsonSerializerSettingsfor each JsonConvert, and you can set a global default.

您可以JsonSerializerSettings为每个指定JsonConvert,并且可以设置全局默认值。

Single JsonConvertwith an overload:

SingleJsonConvert过载:

// Option #1.
JsonSerializerSettings config = new JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore };
this.json = JsonConvert.SerializeObject(YourObject, Formatting.Indented, config);

// Option #2 (inline).
JsonConvert.SerializeObject(YourObject, Formatting.Indented,
    new JsonSerializerSettings() {
        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    }
);

Global Settingwith code in Application_Start()in Global.asax.cs:

Application_Start()在 Global.asax.cs 中使用代码进行全局设置

JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
     Formatting = Newtonsoft.Json.Formatting.Indented,
     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};

Reference: https://github.com/JamesNK/Newtonsoft.Json/issues/78

参考:https: //github.com/JamesNK/Newtonsoft.Json/issues/78

回答by AEMLoviji

Answer is adding this 2 lines of code to Global.asax.cs Application_Start method

答案是将这 2 行代码添加到 Global.asax.cs Application_Start 方法

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.All;

Reference: Handling Circular Object References

参考:处理循环对象引用