【问题标题】:How to keep the property names?如何保留属性名称?
【发布时间】:2015-10-13 22:30:10
【问题描述】:

我有一个类对象:

public class ChildDetails
{
    [JsonProperty("idConsecutivo")]
    public int SequentialId { get; set; }
    [JsonProperty("idSemilla")]
    public int ParentId { get; set; }
    [JsonProperty("idParentSemilla")]
    public int ChildId { get; set; }
    [JsonProperty("NombreArchivo")]
    public string FileName { get; set; }        
}

当执行此行时:

var childItems = JsonConvert.DeserializeObject<List<ChildDetails>>(resultContent);

childItems 对象属性名称为:SequentialId、ParentId、ChildId 和 FileName

但是当执行这一行时:

var otherChildItems = JsonConvert.SerializeObject(childItems);

otherChildItems 对象属性名称为:idConsecutivo、idSemilla、idParentSemilla、NombreArchivo

对象序列化时如何保留SequentialId、ParentId、ChildId和FileName的属性名

谢谢

【问题讨论】:

标签: c# json serialization deserialization


【解决方案1】:

要保持常规属性名称,请不要添加允许您指定替代名称的JsonProperty 属性:

public class ChildDetails
{
    public int SequentialId { get; set; }
    ...
}

【讨论】:

  • 谢谢阿列克谢列文科夫
猜你喜欢
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 2020-03-31
  • 2015-02-01
  • 1970-01-01
  • 2017-09-25
相关资源
最近更新 更多