gpt4 book ai didi

c# - 如何在两个不同的类上使用相同的 JsonProperty 名称

转载 作者:太空宇宙 更新时间:2023-11-03 20:53:25 28 4
gpt4 key购买 nike

如何在两个不同的类上使用相同的 jsonProperty 名称。是否可以。请告知我如何实现这一目标。谢谢

    [JsonProperty("Data")]
public RegisteredMailData[] RegisteredMailData { get; set; }

[JsonProperty("Data")]
public SendRecieveShipmentData[] SendRecieveShipmentData { get; set; }

编辑 - 添加模型以便更好地理解

public partial class MailRoomList
{
[JsonProperty("GetListItemsResult")]
public GetListItemsResult GetListItemsResult { get; set; }
}


public partial class GetListItemsResult
{
[JsonProperty("Data")]
public RegisteredMailData[] RegisteredMailData { get; set; }

[JsonProperty("Data")]
public SendRecieveShipmentData[] SendRecieveShipmentData { get; set; }


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

[JsonProperty("Result")]
public bool Result { get; set; }
}

public class SendRecieveShipmentData
{

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

[JsonProperty("Address")]
public object Address { get; set; }

[JsonProperty("Company Name")]
public string CompanyName { get; set; }

[JsonProperty("Doc Type")]
public object DocType { get; set; }

[JsonProperty("Line Manager Approva")]
public string LineManagerApprova { get; set; }

[JsonProperty("Material_x0028_Detail_x0029_")]
public object Material_x0028_Detail_x0029_ { get; set; }

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

[JsonProperty("PKG Type")]
public object PKGType { get; set; }

[JsonProperty("Request Category")]
public object RequestCategory { get; set; }

[JsonProperty("Sender Address")]
public string SenderAddress { get; set; }

[JsonProperty("Sender Company")]
public object SenderCompany { get; set; }

[JsonProperty("Sender Name")]
public string SenderName { get; set; }

[JsonProperty("Sender Phone")]
public string SenderPhone { get; set; }

[JsonProperty("Sender Zip Code")]
public object SenderZipCode { get; set; }

[JsonProperty("Shipping Companies")]
public string ShippingCompanies { get; set; }

[JsonProperty("Shipping Services")]
public string ShippingServices { get; set; }

[JsonProperty("Telephone")]
public object Telephone { get; set; }

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

[JsonProperty("ZIP Code")]
public object ZIPCode { get; set; }
}

public partial class RegisteredMailData
{
[JsonProperty("__type")]
public string Type { get; set; }

[JsonProperty("Collecting Date")]
public string CollectingDate { get; set; }

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

[JsonProperty("Designee ين_x0")]
public string Designeeين_X0 { get; set; }

[JsonProperty("Mobile phone")]
public string MobilePhone { get; set; }

[JsonProperty("Receipent Date")]
public string ReceipentDate { get; set; }

[JsonProperty("Shipping Companies")]
public string ShippingCompanies { get; set; }

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

[JsonProperty("Tel Number")]
public string TelNumber { get; set; }

[JsonProperty("Title")]
public object Title { get; set; }
}

最佳答案

这似乎是一个可以使用泛型解决的 XY 问题

public partial class MailRoomList<T> {
[JsonProperty("GetListItemsResult")]
public GetListItemsResult<T> GetListItemsResult { get; set; }
}

public class GetListItemsResult<T> {
[JsonProperty("Data")]
public T[] Data { get; set; }

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

[JsonProperty("Result")]
public bool Result { get; set; }
}

反序列化时可以这样做

var result = JsonConvert.DeserializeObject<MailRoomList<RegisteredMailData>>(json);

并可以访问所需的类型数组

关于c# - 如何在两个不同的类上使用相同的 JsonProperty 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52967421/

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