反序列化 json 到 c# 中的对象不映射值
Deserialize json to object in c# does not map values
我正在尝试使用 Jsonconverter 反序列化 json 字符串。我有 json 中的所有值,我还想提一下,特定的 json 是从 swagger 生成的,所以当我向 API 发送有效负载时,它会映射它进入对象模型。
但是,当我尝试将它反序列化为同一个对象时,一切都是空的。我做错了什么?
这就是我反序列化它的方式
EmailUrlDto testEmailUrlDto = JsonConvert.DeserializeObject<EmailUrlDto>(jsonString);
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"
}
]
}
EmailUrlDto
public class EmailUrlDto : EmailDto
{
[Required]
[JsonPropertyName("templateUrl")]
public string TemplateUrl { get; set; }
[Required]
[JsonPropertyName("paramsHtml")]
public ParamsHtmlTemplateDto[] ParamsHtml { get; set; }
}
EmailDto
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; }
}
您用来命名属性的属性来自 System.Text.Json.Serialization
命名空间(例如 JsonPropertyName
),并在 [=19] 期间被 System.Web.Script.Serialization.JavaScriptSerializer
class 使用=].
您还应该添加 Newtonsoft 的 JsonProperty(string)
属性,它应该会按预期工作
[JsonProperty("from")]
[JsonPropertyName("from")]
[MaxLength(250)]
public string From { get; set; }
我正在尝试使用 Jsonconverter 反序列化 json 字符串。我有 json 中的所有值,我还想提一下,特定的 json 是从 swagger 生成的,所以当我向 API 发送有效负载时,它会映射它进入对象模型。
但是,当我尝试将它反序列化为同一个对象时,一切都是空的。我做错了什么?
这就是我反序列化它的方式
EmailUrlDto testEmailUrlDto = JsonConvert.DeserializeObject<EmailUrlDto>(jsonString);
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"
}
]
}
EmailUrlDto
public class EmailUrlDto : EmailDto
{
[Required]
[JsonPropertyName("templateUrl")]
public string TemplateUrl { get; set; }
[Required]
[JsonPropertyName("paramsHtml")]
public ParamsHtmlTemplateDto[] ParamsHtml { get; set; }
}
EmailDto
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; }
}
您用来命名属性的属性来自 System.Text.Json.Serialization
命名空间(例如 JsonPropertyName
),并在 [=19] 期间被 System.Web.Script.Serialization.JavaScriptSerializer
class 使用=].
您还应该添加 Newtonsoft 的 JsonProperty(string)
属性,它应该会按预期工作
[JsonProperty("from")]
[JsonPropertyName("from")]
[MaxLength(250)]
public string From { get; set; }