Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

c# - JavaScriptSerializer-枚举的JSON序列化为字符串(JavaScriptSerializer - JSON serialization of enum as string)

I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer , my json result contains the integer value of the enumeration rather than its string "name".

(我有一个包含enum属性的类,并在使用JavaScriptSerializer序列化对象后,我的json结果包含枚举的整数值,而不是其string “ name”。)

Is there a way to get the enum as a string in my json without having to create a custom JavaScriptConverter ?

(有没有一种方法可以在我的json中将枚举作为string获取而无需创建自定义JavaScriptConverter ?)

Perhaps there's an attribute that I could decorate the enum definition, or object property, with?

(也许有一个属性可以修饰enum定义或对象属性?)

As an example:

(举个例子:)

enum Gender { Male, Female }

class Person
{
    int Age { get; set; }
    Gender Gender { get; set; }
}

Desired json result:

(所需的json结果:)

{ "Age": 35, "Gender": "Male" }

Ideally looking for answer with built-in .NET framework classes, if not possible alternatives (like Json.net) are welcome.

(理想情况下,使用内置的.NET框架类寻找答案,如果可能的话,欢迎使用替代方法(如Json.net)。)

  ask by Omer Bokhari translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have found that Json.NET provides the exact functionality I'm looking for with a StringEnumConverter attribute:

(我发现Json.NET通过StringEnumConverter属性提供了我正在寻找的确切功能:)

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }

More details at available on StringEnumConverter documentation .

(有关更多详细信息,请StringEnumConverter文档 。)

There are other places to configure this converter more globally:

(还有其他地方可以更全局地配置此转换器:)

  • enum itself if you want enum always be serialized/deserialized as string:

    (枚举本身,如果您想让枚举始终被序列化/反序列化为字符串:)

     [JsonConverter(typeof(StringEnumConverter))] enum Gender { Male, Female } 
  • In case anyone wants to avoid attribute decoration, you can add the converter to your JsonSerializer (suggested by Bj?rn Egil ):

    (如果有人想避免装修的属性,你可以转换器添加到您的JsonSerializer(所建议比约恩埃吉尔 ):)

     serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); 

    and it will work for every enum it sees during that serialization (suggested by Travis ).

    (并且它将对在序列化期间看到的每个枚举都有效(由Travis建议)。)

  • or JsonConverter (suggested by banana ):

    (或JsonConverter(由香蕉建议):)

     JsonConvert.SerializeObject(MyObject, new Newtonsoft.Json.Converters.StringEnumConverter()); 

Additionally you can control casing and whether numbers are still accepted by using StringEnumConverter(NamingStrategy, Boolean) constructor.

(另外,您可以使用StringEnumConverter(NamingStrategy,Boolean)构造函数来控制大小写以及是否仍接受数字。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...