gpt4 book ai didi

c# - 复杂的 Json 到 C# 对象反序列化类

转载 作者:行者123 更新时间:2023-12-05 08:05:28 39 4
gpt4 key购买 nike

我正在尝试反序列化从网络调用中获得的 json 响应。我已经知道了 90%。我很难弄清楚的唯一部分是这些 json 数组中包含数据,并且每个数组名称使用电子邮件地址都是唯一的。我一直无法弄清楚如何将电子邮件数组转换为 1. 动态并让它创建许多列表或仅创建几个列表,具体取决于响应中返回的内容,并动态命名列表数组以将数据放入记录中类。

正如您在 Records 类中看到的那样,我需要它更加动态和灵活以接收任何和所有电子邮件。

下面是json:

{
"result": {
"records": {
"joeblow@gmail.com": [
{
"OrderId": "d9535109-d305-4584-a503-8194bbcfcff2",
"CompletedOrderId": "BCFCFF2",
"CustomerId": 1212,
"CompletedTime": "2020-10-26 13:32:02",
"Email": "joeblow@gmail.com",
"ShippingFirstName": "Joe",
"ShippingMiddleName": "",
"ShippingLastName": "Blow",
"LineItems": {
"tfl.es.bluray": { "qty": 1 },
"booklets.en.ebook": { "qty": 1 }
}
}
],
"cleob@hotmail.com": [
{
"OrderId": "7bf97b3a-bc46-411c-bc30-12563326dba0",
"CompletedOrderId": "326DBA0",
"CustomerId": 1212,
"CompletedTime": "2020-10-26 20:07:44",
"Email": "cleob@hotmail.com",
"ShippingFirstName": "Cleo",
"ShippingMiddleName": "",
"ShippingLastName": "Blue",
"LineItems": {
"tfl.es.bluray": { "qty": 1 },
"booklets.en.ebook": { "qty": 1 },
"aos.en.pb": { "qty": 1 },
"course-tos.en.olr": { "qty": 1 },
"pow-hsk-nofilm.en.combo": { "qty": 1 },
"course-organizing.en.olr": { "qty": 1 }
}
}
],
"johnd@gmail.com": [
{
"OrderId": "630f0dda-94c3-4b82-a070-2554004dce29",
"CompletedOrderId": "04DCE29",
"CustomerId": 12345,
"CompletedTime": "2020-10-25 21:52:04",
"Email": "johnd@gmail.com",
"ShippingFirstName": "John",
"ShippingMiddleName": "",
"ShippingLastName": "Doe",
"LineItems": {
"tfl.es.bluray": { "qty": 1 },
"booklets.en.ebook": { "qty": 1 },
"aos.en.pb": { "qty": 1 },
"course-tos.en.olr": { "qty": 1 },
"pow-hsk-nofilm.en.combo": { "qty": 1 },
"course-organizing.en.olr": { "qty": 1 },
"oak-2007.en.cd": { "qty": 1 }
}
}
]
},
"errors": [
{
"id": "bademailaddress-yahoo.com",
"message": "Email address 'bademailaddress-yahoo.com' is not a valid email address"
}
]
},
"jsonrpc": "2.0",
"id": 12345634523
}

以及我为 json 反序列化制作的类:

public partial class JsonEmailDeSerializer
{
[JsonProperty("result")]
public Result Result { get; set; }

[JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; }

[JsonProperty("id")]
public long Id { get; set; }
}

public partial class Result
{
[JsonProperty("records")]
public Records Records { get; set; }

[JsonProperty("errors")]
public List<Error> Errors { get; set; }
}

public partial class Error
{
[JsonProperty("id")]
public string Id { get; set; }

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

public partial class Records
{
[JsonProperty("joeblow@gmail.com")]
public List<MailCom> JoeblowGmailCom { get; set; }

[JsonProperty("cleob@hotmail.com")]
public List<MailCom> CleobHotmailCom { get; set; }

[JsonProperty("johnd@gmail.com")]
public List<MailCom> JohndGmailCom { get; set; }
}

public partial class MailCom
{
[JsonProperty("OrderId")]
public Guid OrderId { get; set; }

[JsonProperty("CompletedOrderId")]
public string CompletedOrderId { get; set; }

[JsonProperty("CustomerId")]
public long CustomerId { get; set; }

[JsonProperty("CompletedTime")]
public DateTimeOffset CompletedTime { get; set; }

[JsonProperty("Email")]
public string Email { get; set; }

[JsonProperty("ShippingFirstName")]
public string ShippingFirstName { get; set; }

[JsonProperty("ShippingMiddleName")]
public string ShippingMiddleName { get; set; }

[JsonProperty("ShippingLastName")]
public string ShippingLastName { get; set; }

[JsonProperty("LineItems")]
public Dictionary<string, LineItem> LineItems { get; set; }
}

public partial class LineItem
{
[JsonProperty("qty")]
public long Qty { get; set; }
}

public partial class JsonEmailDeSerializer
{
public static JsonEmailDeSerializer FromJson(string json) => JsonConvert.DeserializeObject<JsonEmailDeSerializer>(json, FedExShipper.Converter.Settings);
}

public static class Serialize
{
public static string ToJson(this JsonEmailDeSerializer self) => JsonConvert.SerializeObject(self, FedExShipper.Converter.Settings);
}

internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}

最佳答案

使用动态属性名称反序列化 json 的常用方法是使用 Dictionary<string, ...> , 在这种情况下 - Dictionary<string, List<MailCom>>可用于 Records属性:

public partial class Result
{
[JsonProperty("records")]
public Dictionary<string, List<MailCom>> Records { get; set; }

[JsonProperty("errors")]
public List<Error> Errors { get; set; }
}

Json.NET 可以处理 json 对象属性 as keys for dictionary这使得它适合反序列化此类动态数据。 System.Text.Json也是如此.

关于c# - 复杂的 Json 到 C# 对象反序列化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65118056/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com