正在WCF REST中设置JSON对象名称
本文关键字:对象 JSON 设置 WCF REST 正在 | 更新日期: 2023-09-27 18:29:36
我正在发送JSON:
[
{
"Description": "A kud ces bolje",
"ItemID": 1,
"Name": "Banane",
"NewPrice": 91.99,
"OldPrice": 161.99,
"SaleEnds": "9/1/2013",
"SaleStarts": "9/1/2012"
},
{
"Description": "Rastu na stablu",
"ItemID": 2,
"Name": "Kruske",
"NewPrice": 93.99,
"OldPrice": 117.99,
"SaleEnds": "9/1/2013",
"SaleStarts": "9/1/2012"
},
]
但我想有一个名字,让它看起来像
"Some_Name":[
{
"Description": "A kud ces bolje",
"ItemID": 1,
"Name": "Banane",
"NewPrice": 91.99,
"OldPrice": 161.99,
"SaleEnds": "9/1/2013",
"SaleStarts": "9/1/2012"
},
{
"Description": "Rastu na stablu",
"ItemID": 2,
"Name": "Kruske",
"NewPrice": 93.99,
"OldPrice": 117.99,
"SaleEnds": "9/1/2013",
"SaleStarts": "9/1/2012"
},
]
我返回List<Item>
:
[OperationContract]
[WebGet(UriTemplate = "/items/{id}", ResponseFormat = WebMessageFormat.Json)]
List<ItemService> GetItems(string id);
CCD_ 2为
[DataContract]
public class ItemService
{
[DataMember]
public int ItemID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public Decimal OldPrice { get; set; }
[DataMember]
public Decimal NewPrice { get; set; }
[DataMember]
public string SaleStarts { get; set; }
[DataMember]
public string SaleEnds { get; set; }
}
您不能命名JSon数据本身。您需要一个包含名称的小型包装器类:
class Wrapper { public Some_Name List<ItemService> {get;set;}
在包装器中包装您的列表并返回包装器。
由此产生的JSon有点像:
{
"Some_Name":[
{
"Description": "A kud ces bolje",
"ItemID": 1,
"Name": "Banane",
"NewPrice": 91.99,
"OldPrice": 161.99,
"SaleEnds": "9/1/2013",
"SaleStarts": "9/1/2012"
},
{
"Description": "Rastu na stablu",
"ItemID": 2,
"Name": "Kruske",
"NewPrice": 93.99,
"OldPrice": 117.99,
"SaleEnds": "9/1/2013",
"SaleStarts": "9/1/2012"
},
]}