我正在尝试创建一个公共类,我可以在我的请求中使用它作为公共类。在request/payload
下面,我有"名称"," sku","数量"," unit_price"和" total_price"作为重复的领域。
当我直接声明它们时(如选项-1所示),它可以正常工作。现在我正在尝试优化代码(选项-2),但在这种情况下,我得到null
。我正在学习RESTful Web API调用并对此感到好奇。
问题:
请求有效负载:
"item":
{
"type": "Combo",
"name": "The King Double Egg King Menu",
"sku": null,
"quantity": 1,
"unit_price": 1890,
"total_price": 2030,
"sub_items": [
{
"type": "Product",
"name": "The King Double Egg",
"sku": null,
"quantity": 1,
"unit_price": 0,
"total_price": 140,
"modifiers": [
{
"type": "Extra",
"name": "Bacon",
"sku": null,
"quantity": 1,
"unit_price": 100,
"total_price": 100
}]
}]
}
选项 - 1(本作品):
[DataContract]
public class Item
{
[DataMember(Name = "type")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public OrderItemType Type { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "sku")]
public string Sku { get; set; }
[DataMember(Name = "quantity")]
public int? Quantity { get; set; }
[DataMember(Name = "unit_price")]
public int UnitPrice { get; set; }
[DataMember(Name = "total_price")]
public int TotalPrice { get; set; }
[DataMember(Name = "sub_items")]
public List<Item> SubItems { get; set; }
[DataMember(Name = "modifiers")]
public List<OrderItemModifier> Modifiers { get; set; }
}
[DataContract]
public class OrderItemModifier
{
[DataMember(Name = "type")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public OrderModifierType Type { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "sku")]
public string Sku { get; set; }
[DataMember(Name = "quantity")]
public int? Quantity { get; set; }
[DataMember(Name = "unit_price")]
public int UnitPrice { get; set; }
[DataMember(Name = "total_price")]
public int TotalPrice { get; set; }
}
选项 - 2(尝试为常见对象创建一个类,但它不起作用):
[DataContract]
public class Item
{
[DataMember(Name = "type")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public OrderItemType Type { get; set; }
public Details Details { get; set; }
[DataMember(Name = "sub_items")]
public List<Item> SubItems { get; set; }
}
[DataContract]
public class OrderItemModifier
{
[DataMember(Name = "type")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public OrderModifierType Type { get; set; }
public Details ModifierDetails { get; set; }
}
[DataContract]
public class Details
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "sku")]
public string Sku { get; set; }
[DataMember(Name = "quantity")]
public int? Quantity { get; set; }
[DataMember(Name = "unit_price")]
public int UnitPrice { get; set; }
[DataMember(Name = "total_price")]
public int TotalPrice { get; set; }
}
答案 0 :(得分:1)
第二个示例将匹配您在此处显示的不同JSON。它将匹配以下JSON:
"item":
{
"type": "Combo",
"details":
{
"name": "The King Double Egg King Menu",
"sku": null,
"quantity": 1,
"unit_price": 1890,
"total_price": 2030
},
"sub_items": [
{
"type": "Product",
"modifierdetails":
{
"name": "The King Double Egg",
"sku": null,
"quantity": 1,
"unit_price": 0,
"total_price": 140
},
"modifiers": [
{
"type": "Extra",
"name": "Bacon",
"sku": null,
"quantity": 1,
"unit_price": 100,
"total_price": 100
}]
}]
}
所以你要添加一个额外的属性细节和modifierdetails。
更好的选择是使用intheritance,例如:
[DataContract]
public class Details
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "sku")]
public string Sku { get; set; }
[DataMember(Name = "quantity")]
public int? Quantity { get; set; }
[DataMember(Name = "unit_price")]
public int UnitPrice { get; set; }
[DataMember(Name = "total_price")]
public int TotalPrice { get; set; }
}
[DataContract]
public class Item : Details
{
[DataMember(Name = "type")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public OrderItemType Type { get; set; }
[DataMember(Name = "sub_items")]
public List<Item> SubItems { get; set; }
}
[DataContract]
public class OrderItemModifier : Details
{
[DataMember(Name = "type")]
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public OrderModifierType Type { get; set; }
}