在序列化时,我应该更改什么设置来获取枚举的int值而不是其字符串表示形式? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

在序列化对象时,我应该更改什么设置来获取枚举的属性值而不是其字符串表示?我有以下课程。

public class ProductModel
{
    public long ProductId { get; set; }

    public int ContainerType { get; set; }

    public SolidForm SolidForm { get; set; }
}

(例如)现在--->我的json =

{ "ProductId" : 22222,
  "ContainerType" : 1111,
  "SolidForm" : "Solid"
}

但序列化后我需要这个。 (不作为字符串枚举)

{ "ProductId" : 22222,
  "ContainerType" : 1111,
  "SolidForm" : 1
}

我希望我的对象中的所有枚举转换为int。

这是我对Json Serialization的设置

JsonSerializerSettings = new JsonSerializerSettings
    {
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
        Error = delegate (object sender, ErrorEventArgs args)
        {
            args.ErrorContext.Handled = true;
        }
    }
c# json enums json.net refit
1个回答
1
投票

Newtonsoft.Json中的默认值是将枚举序列化为int。假设你的意思是Newtonsoft.Json。

你的枚举是用[JsonConverter(typeof(StringEnumConverter))]属性装饰的吗?

© www.soinside.com 2019 - 2024. All rights reserved.