使用 C#、Newtonsoft.Json 13.0.2 并具有以下可能的 JSON:
Json 类型 1 ---------------------
{
"type": "succeeded",
"event_date": "2023",
"transaction": {
"id": "AAA",
"authorization": "123",
"operation_type": "456"
}
}
Json 类型 2 ---------------------
{
"type": "failed",
"event_date": "2023",
"failureReport": {
"id": "AAA",
"failureType": "123"
}
}
我使用以下代码来反序列化两个对象中的任何一个,如下所示:
[Table("MySql_Test_Table")]
public class TestClass
{
[Key]
[JsonPropertyName("id")]
public int id { get; set; }
[JsonPropertyName("event_date")]
public DateTime? event_date { get; set; }
[JsonPropertyName("type")]
public String? type { get; set; }
[JsonPropertyName("transaction.id")]
public String? transaction_id { get; set; }
[JsonPropertyName("transaction.authorization")]
public String? authorization{ get; set; }
[JsonPropertyName("transaction.operation_type")]
public String? operation_type{ get; set; }
[JsonPropertyName("failureReport.id")]
public String? failureReportId{ get; set; }
[JsonPropertyName("failureReport.failureType")]
public String? failureReportType{ get; set; }
}
TestClass testClass = Newtonsoft.Json.JsonConvert.DeserializeObject<TestClass>(oneOfTwoPossiblejsonTexts.ToString());
//...Code for saving testClass using EntityFramrwork...
使用上述代码,我可以毫无问题地获取 type 和 event_date 的值。但是我无法填充 id、授权、操作类型、failureReportId 或failureReportType。 我想问题是我的 [JsonPropertyName] 标签。请问,我该如何更正它以访问所需的值?
如果你想将这些 json 反序列化到你的 Flatten 类中,你可以创建一个 JsonConstructor
public class TestClass
{
public int id { get; set; }
[JsonProperty("event_date")]
public string? eventDate { get; set; }
public String? type { get; set; }
public String? transactionId { get; set; }
public String? authorization { get; set; }
public String? operationType { get; set; }
public String? failureReportId { get; set; }
public String? failureReportType { get; set; }
[JsonConstructor]
public TestClass(JToken transaction, JToken failureReport)
{
if (transaction != null)
{
transactionId = (string?)transaction["id"];
authorization = (string?)transaction["authorization"];
operationType = (string?)transaction["operation_type"];
};
if (failureReport != null)
{
failureReportId = (string?)failureReport["id"];
failureReportType = (string?)failureReport["failureType"];
};
}
public TestClass() { }
}
由于您使用 Newtonsoft.Json 进行反序列化,因此必须使用 [JsonProperty] 而不是 [JsonPropertyName]
最安全的选择可能是创建相应的 C# 类型:
public class TestClass
{
[Key]
[JsonPropertyName("id")]
public int id { get; set; }
[JsonPropertyName("event_date")]
public DateTime? event_date { get; set; }
[JsonPropertyName("type")]
public String? type { get; set; }
public TransactionType Transaction { get; set; }
public FailureReportType FailureReport { get; set; }
}
public class TransactionType
{
[JsonPropertyName("id")]
public String? transaction_id { get; set; }
[JsonPropertyName("authorization")]
public String? authorization{ get; set; }
[JsonPropertyName("operation_type")]
public String? operation_type{ get; set; }
}
public class FailureReportType
{
[JsonPropertyName("id")]
public String? failureReportId{ get; set; }
[JsonPropertyName("failureType")]
public String? failureReportType{ get; set; }
}
在我的例子中,我必须获得
name.givenName
和 name.familyName
嵌套属性,我发现这个解决方案对于 System.Text.Json
来说是最简单的(无需为 Name
创建额外的类):
[JsonPropertyName("name")]
public Dictionary<string, string> Name { get; set; }
public string FirstName { get => Name["givenName"]; }
public string LastName { get => Name["familyName"]; }