Web API returns JSon format

.NET Web API 中

surroundings:

Newtonsoft.Json v9.0.0.1

1. Global configuration:

  APPlication_Start arranged following code () can return all fields JSon camelCased are converted to:

GlobalConfiguration.Configure.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

After applying the global configuration, if not JsonProperty add NamingStrategy, the JsonProperty the Name statement will also be applied to hump format can be configured to achieve the following exception to configure custom

2. Custom configuration:

  After inheriting NamingStrategy, implementation GetPropertyName exception configuration can be achieved, and ultimately returned Json string format is this method GetPropertyName return value.

3. With regard to custom configuration of analysis:

Call path:

DefaultContractResolver..CreateObjectContract()->CreateProperties()->遍历Member, 调用CreateProperty(): JsonProperty property = CreateProperty(member, memberSerialization);->SetPropertySettingsFromAttributes()->

In the method SetPropertySettingsFromAttributes () in 

  1. If there JsonPropertyAttribute / JsonContainerAttribute characteristics, and the attribute NamingStrategy-> getPropertyName method call () ;

  2. Otherwise call ResolvePropertyName, which is DefaultContractResolver comes with NamingStrategy property (CamelCasePropertyNamesContractResolver to achieve their NamingStrategy, DefaultContractResolver of NamingStrategy is empty, but can be assigned externally).

  3. In other words, you can customize the format DefaultContractResolver assignment:

var resolver = new DefaultContractResolver();
resolver.NamingStrategy = new TestStrategy();
config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
    ContractResolver = resolver
};
Call namingStrategy Source:
if (namingStrategy != null)
{
    property.PropertyName = namingStrategy.GetPropertyName(mappedName, hasSpecifiedName);
}
else
{
    property.PropertyName = ResolvePropertyName(mappedName);
}

My simple example:

public class Strategy4JsonProperty : NamingStrategy
{
    protected override string ResolvePropertyName(string name)
    {
        throw new NotImplementedException();
    }

    public override string GetPropertyName(string name, bool hasSpecifiedName)
    {
        return name + "!!!";
    }
}

[JsonObject(NamingStrategyType = typeof(Strategy4JsonProperty))]
public partial class ResearchList
{

    public Guid Id { get; set; }
    public string Name { get; set; }

}
public partial class TEST
{
    [JsonProperty(NamingStrategyType = typeof(Strategy4JsonProperty))]
    public Guid Id { get; set; }
    public string Name { get; set; }

}
View Code

Conclusion: The global configuration with external, internal characteristics plus strategy, you can achieve different types of return Json format.

Guess you like

Origin www.cnblogs.com/duliduibai/p/11311086.html