JsonSerializerSettings

JsonSerializerSettings

本文介绍了如何设置自定义JsonSerializerSettings为Json.NET在MVC 4的Web API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白的ASP.NET Web API本身使用Json.NET的(反)序列化对象,但有没有指定要为 JsonSerializerSettings 对象的方法它使用?

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

例如,如果我希望包括键入信息到系列化的JSON字符串?一般情况下我注入设置到 .Serialize()电话,但网页API这是否默默地。我不能找到一种方法,手动注入设置。

For example, what if I wanted to include type information 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.

推荐答案

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

You can customize the JsonSerializerSettings by using the Formatters.JsonFormatter.SerializerSettings property in the HttpConfiguration object.

例如,你可以做到这一点在Application_Start()方法:

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

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

这篇关于如何设置自定义JsonSerializerSettings为Json.NET在MVC 4的Web API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:40