【问题标题】:Allow JSON Input for Parameter, but do not Serialize in Output允许参数的 JSON 输入,但不要在输出中序列化
【发布时间】:2015-03-23 16:21:25
【问题描述】:

我需要一个 .NET Web API,它允许从 JSON 对象输入字段,但不会将其序列化。我已经尝试过 [JsonIgnore] 注释,但是对于任何 json 对象,它都会完全忽略它,包括输入。

型号

public class MyModel{
    [Key]
    public int id {get; set;}
    public string name {get; set;}
    [JsonIgnore]
    public Byte[] file {get; set;}
}

Json 输入

{
    "name" : "MyName",
    "image" : "akjsfjkha37842hui23yh23b"
}

期望的输出

{
    "name" : "MyName"
}

【问题讨论】:

    标签: .net json serialization asp.net-web-api annotations


    【解决方案1】:

    为此,您应该使用 ShouldSerialize[field] 方法。 Json.NET 通过查找方法来判断是否将某个字段序列化为 JSON。使用以下将阻止“图像”序列化:

    public bool ShouldSerializeimage(){
    return false;
    }
    

    这应该放在模型中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      相关资源
      最近更新 更多