c# 中将 json 反序列化为对象不映射值

Csi*_*ert 3 c# json json.net jsonconverter jsonconvert

我正在尝试使用 Jsonconverter 反序列化 json 字符串。我拥有 json 中的所有值,我还想提一下,特定的 json 是从 swagger 生成的,因此当我向 API 发送有效负载时,它会将其映射到对象模型中。

但是,当我尝试将其反序列化为同一个对象时,一切都是空的。我做错了什么?

这就是我反序列化它的方式

EmailUrlDto testEmailUrlDto = JsonConvert.DeserializeObject<EmailUrlDto>(jsonString);
Run Code Online (Sandbox Code Playgroud)

JSON格式

  {
    "templateUrl": "https://some.blob.url.here.com",
    "paramsHtml": [
  {
    "name": "{{miniAdmin}}",
    "value": "Heyth is Admin!"
  },
  {
   "name": "{{cliBuTree}}",
   "value": "I`m a treeee!"
 }
 ],
 "from": "string",
 "subject": "string",
 "message": "string",
 "isHtmlBody": true,
 "recipients": [
   {
     "recipient": "some.name@lol.com"
   }
 ],
  "cCRecipients": [
    {
      "recipient": "string"
    }
  ],
  "bCCRecipients": [
    {
      "recipient": "string"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

电子邮件网址

public class EmailUrlDto : EmailDto
{
    [Required]
    [JsonPropertyName("templateUrl")]
    public string TemplateUrl { get; set; }

    [Required]
    [JsonPropertyName("paramsHtml")]
    public ParamsHtmlTemplateDto[] ParamsHtml { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

电子邮件Dto

public class EmailDto : Dto
{

    [JsonIgnore]
    public int Id { get; set; }


    [JsonPropertyName("from")]
    [MaxLength(250)]
    public string From { get; set; }


    [JsonPropertyName("subject")]
    [Required]
    [MaxLength(300)]
    public string Subject { get; set; }


    [JsonPropertyName("message")]
    [Required]
    public string Message { get; set; }


    [JsonPropertyName("isHtmlBody")]
    public bool IsHtmlBody { get; set; }


    [JsonIgnore]
    public EStatus EmlStatus { get; set; }


    [JsonPropertyName("smtp")]
    public SMTPConfigurationDto Smtp { get; set; }


    [JsonIgnore]
    public AttachmentDto[] Attachments { get; set; }


    [JsonPropertyName("recipients")]
    public ToRecipientDto[] Recipients { get; set; }


    [JsonPropertyName("cCRecipients")]
    public CcRecipientDto[] CCRecipients { get; set; }


    [JsonPropertyName("bCCRecipients")]
    public BccRecipientDto[] BCCRecipients { get; set; }


    [JsonIgnore]
    public LogDto[] Logs { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

Ogu*_*gul 9

用于命名属性的属性来自System.Text.Json.Serialization命名空间(JsonPropertyName例如),并System.Web.Script.Serialization.JavaScriptSerializer在序列化/反序列化期间由类使用。

您还应该添加 Newtonsoft 的JsonProperty(string)属性,它应该按预期工作

[JsonProperty("from")]
[JsonPropertyName("from")]
[MaxLength(250)]
public string From { get; set; }
Run Code Online (Sandbox Code Playgroud)