C#中[JsonProperty]特性的作用是什么?附代码解析与拓展用法问询
[JsonProperty]特性的详解 Hey there! Let's break down exactly what the [JsonProperty] attribute does in C#, using your example to make it concrete, and then cover some of its handy extended uses.
核心作用
First off, [JsonProperty] is a key attribute from the Newtonsoft.Json (Json.NET) library (the most popular JSON serializer/deserializer for C#). Its main job is to control how a C# class property maps to a JSON field. By default, Json.NET matches property names directly (case-sensitive, though you can tweak that globally), but this attribute lets you override that behavior entirely.
你的代码示例为什么需要它?
Looking at your code:
public class FileAttachment { [JsonProperty("fileName")] public string FileName{ get; set; } }
Your C# property uses PascalCase (FileName), but the corresponding JSON field uses camelCase (fileName). Without [JsonProperty], Json.NET would serialize FileName to "FileName" in JSON, and when deserializing, it would only look for a JSON field named "FileName"—which wouldn't match the expected "fileName". This would lead to missing data or failed deserialization.
Adding [JsonProperty("fileName")] explicitly tells Json.NET: "Hey, when you serialize this property, name the JSON key fileName; when you deserialize, map the fileName JSON field to this property." That way, the C# naming conventions and JSON naming conventions play nicely together.
拓展用法
This attribute has a ton of useful tricks beyond just renaming fields:
Set default values for missing fields: Use
DefaultValueHandlingto populate properties with a default value if the JSON doesn't include the field. For example:[JsonProperty("fileName", DefaultValueHandling = DefaultValueHandling.Populate)] [DefaultValue("unknown.txt")] public string FileName { get; set; }Now if the JSON has no
fileNamefield,FileNamewill automatically be set to"unknown.txt".Ignore null values in serialization: Use
NullValueHandlingto skip writing null properties to JSON, which reduces payload size:[JsonProperty("fileName", NullValueHandling = NullValueHandling.Ignore)] public string FileName { get; set; }If
FileNameis null, it won't show up in the serialized JSON.Enforce required fields: Mark a property as mandatory with
Required. If the JSON is missing this field during deserialization, Json.NET will throw an exception:[JsonProperty("fileName", Required = Required.Always)] public string FileName { get; set; }Great for ensuring critical data isn't missing.
Control JSON field order: Use
Orderto specify the order of fields in the serialized JSON. Lower numbers come first:[JsonProperty("id", Order = 1)] public int Id { get; set; } [JsonProperty("fileName", Order = 2)] public string FileName { get; set; }This will put
"id"before"fileName"in the JSON output.Support multiple JSON field aliases: You can apply
[JsonProperty]multiple times to a single property to accept different JSON field names during deserialization:[JsonProperty("fileName")] [JsonProperty("file_name")] public string FileName { get; set; }Now the property will map to either
fileNameorfile_namefrom JSON.
内容的提问来源于stack exchange,提问作者A.Blanc

