用于序列化十进制值的JSON标准未提供本地化格式。(请参阅JSON.org。)这就是为什么始终使用不变文化对值进行格式化的原因。
如果需要本地化的值,则需要为选择的序列化程序创建一个自定义转换器,该转换器将小数部分输出为预格式化的字符串。在Json.Net中,可以轻松完成此操作,如下所示:
class Program{ static void Main(string[] args) { List<decimal> values = new List<decimal> { 1.1M, 3.14M, -0.9M, 1000.42M }; var converter = new FormattedDecimalConverter(CultureInfo.GetCultureInfo("fr-FR")); string json = JsonConvert.SerializeObject(values, converter); Console.WriteLine(json); }}class FormattedDecimalConverter : JsonConverter{ private CultureInfo culture; public FormattedDecimalConverter(CultureInfo culture) { this.culture = culture; } public override bool CanConvert(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(double) || objectType == typeof(float)); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.Writevalue(Convert.ToString(value, culture)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); }}输出:
["1,1","3,14","-0,9","1000,42"]



