我有一个我序列化的DTO类

Json.Serialize(MyClass)

我怎么能排除它public的财产呢?

(It has to be public, as I use it in my code somewhere else)

推荐答案

If you are using Json.Net attribute [JsonIgnore] will simply ignore the field/property while serializing or deserialising.

public class Car
{
  // included in JSON
  public string Model { get; set; }
  public DateTime Year { get; set; }
  public List<string> Features { get; set; }

  // ignored
  [JsonIgnore]
  public DateTime LastModified { get; set; }
}

或者可以使用DataContract和DataMember属性有 Select 地序列化/反序列化属性/字段.

[DataContract]
public class Computer
{
  // included in JSON
  [DataMember]
  public string Name { get; set; }
  [DataMember]
  public decimal SalePrice { get; set; }

  // ignored
  public string Manufacture { get; set; }
  public int StockCount { get; set; }
  public decimal WholeSalePrice { get; set; }
  public DateTime NextShipmentDate { get; set; }
}

Refer http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size for more details

Json相关问答推荐

PowerShell脚本-替换json数组(转义$var将被视为不带双引号的文本)

JSON字符串处理注入引号

Jolt需要将缺少的值设置为空并保持相同的位置

使用PowerShell解析文件并获取特定行的值

如何避免解析 ISuperObject 类型字段中的 json 对象

使用 jolt 变换压平具有公共列 JSON 的复杂嵌套

从包含 JSON 对象序列的文件中获取第一个 JSON 对象

GitHub Pages无法正确显示我的项目

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

通过sql查询读取嵌套Json

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

如何使用 gson 调用默认反序列化

如何一次加载无限滚动中的所有条目以解析python中的HTML

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

使用 JSON 的 javascript 深拷贝

如何使用 Serde 反序列化带有自定义函数的可选字段?

如何在dart Flutter 中将json字符串转换为json对象?

SCRIPT5009:JSON未定义

使用新的 Net Core 3.0 Json 时忽略属性

如何对 Javascript 对象进行排序,或将其转换为数组?