2012-12-11 55 views

回答

3

您可以访问JSON.NET串行器设置。使用JSON.NET,您可以使用转换器来覆盖转换。 this datetime one

你也可以通过从抽象JsonConverter继承来实现你自己的。详情请参阅here

对于示例创建转换器:

public class UpperCaseStringConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(string); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     return reader.Value.ToString(); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     var outputValue = value as string; 
     writer.WriteValue(outputValue == null ? null : outputValue.ToUpper()); 
    } 
} 

然后注册这个全球加此配置:从here

JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings(); 
jSettings.Converters.Add(new UpperCaseStringConverter()); 
jsonFormatter.SerializerSettings = jSettings; 

注册例如要添加到一个单一的财产一个模型只需添加注释:

[JsonConverter(typeof(UpperCaseStringConverter))] 
+0

完美答案。荣誉给你! – Sandro