如何在两个不同的类上使用相同的JsonProperty Name

时间:2018-10-24 11:07:03

标签: c# json.net

如何在两个不同的类上使用相同的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; }
}

2 个答案:

答案 0 :(得分:4)

这似乎是一个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);

并有权访问所需的类型数组

答案 1 :(得分:0)

It's not possible to do it - if you try to serialize a object that has 2 json properties with the same name you'll get a Newtonsoft.Json.JsonSerializationException

Demo code, using your model classes:

var data = new GetListItemsResult
{
    SendRecieveShipmentData = new SendRecieveShipmentData[]
    {
        new SendRecieveShipmentData
        {
            Name = "foo"
        }
    },

    RegisteredMailData = new RegisteredMailData[]
    {
        new RegisteredMailData
        {
            Status = "ok"
        }
    }
};

string json = JsonConvert.SerializeObject(data);

Exception message:

Newtonsoft.Json.JsonSerializationException: 'A member with the name 'Data' already exists on 'ConsoleApp3.GetListItemsResult'. Use the JsonPropertyAttribute to specify another name.'

The solution would be to give different property names, for example:

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

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


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

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