You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中[JsonProperty]特性的作用是什么?附代码解析与拓展用法问询

关于C#中[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 DefaultValueHandling to 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 fileName field, FileName will automatically be set to "unknown.txt".

  • Ignore null values in serialization: Use NullValueHandling to skip writing null properties to JSON, which reduces payload size:

    [JsonProperty("fileName", NullValueHandling = NullValueHandling.Ignore)]
    public string FileName { get; set; }
    

    If FileName is 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 Order to 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 fileName or file_name from JSON.

内容的提问来源于stack exchange,提问作者A.Blanc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:50:47