今天在做項目時遇到了要將json對象序列化為數據庫表, 結果數據庫表和json對象數據類型和字段名稱對不上。於是就有了一下的解決方案:
需要轉化的對象
{
"orgAccountId":-1280880274464620300,
"id":-1280880274464620300,
"name":"事業部"
}
轉化的實體對象
public class BureauOADeptModel { public int Id { get; set; } public string LongId { get; set; } }
目的:將json中的Id序列化到LongId,實體字段中的Id則不需要序列化。
新的實體對象:
public class BureauOADeptModel { [JsonIgnore] public int Id { get; set; } [JsonProperty(PropertyName = "Id")] public string LongId { get; set; } }
知識點:
1.JsonIgnore:忽略,不進行序列化
2.JsonProperty(PropertyName=“XXX”):將指定名稱的屬性序列化到當前屬性