【问题标题】:How to ignore underscore when deserializing [duplicate]反序列化时如何忽略下划线[重复]
【发布时间】:2019-09-05 18:23:47
【问题描述】:

我们有类似 Golang 的 json 注解的东西吗?

type FieldType struct {
    TypeName      string          `json:"typeName"`
    CodeType      string          `json:"codeType"`
    Suffix        string          `json:"suffix"`
    PropertiesRaw json.RawMessage `json:"properties"`
    Properties    FieldTypePropertyMap
}

我有一个 json 字符串:

{ "long_name":"dffdf" }

我的班级:

public class Result
{
  public int LongName {get; set;}
}

由于有下划线,LongName 始终为空,我不想在我的类属性中使用下划线。

反序列化时是否有忽略下划线的选项?

【问题讨论】:

标签: c# json json.net


【解决方案1】:

不要忽略,但您可以使用这样的属性名称进行装饰:

public class Result
{
    [JsonProperty(PropertyName = "long_name")]
    public int LongName { get; set; }
}

【讨论】:

  • 感谢 Travis,我相信这会奏效,我最终选择了 JsonSerializerSettings,因为它更方便。为了微调,将使用这种方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2022-10-13
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多