How to exclude property from Json Serialization

后端 未结 7 1534
粉色の甜心
粉色の甜心 2020-11-22 13:22

I have a DTO class which I Serialize

Json.Serialize(MyClass)

How can I exclude a public property of it?

(It has to

7条回答
  •  醉梦人生
    2020-11-22 13:31

    If you are using System.Text.Json then you can use [JsonIgnore].
    FQ: System.Text.Json.Serialization.JsonIgnoreAttribute

    Official Microsoft Docs: JsonIgnoreAttribute

    As stated here:

    The library is built-in as part of the .NET Core 3.0 shared framework.
    For other target frameworks, install the System.Text.Json NuGet package. The package supports:

    • .NET Standard 2.0 and later versions
    • .NET Framework 4.6.1 and later versions
    • .NET Core 2.0, 2.1, and 2.2

提交回复
热议问题