我如何使用web api发布带有嵌套arraylist的json数据

时间:2011-11-24 06:00:58

标签: json.net wcf-web-api

我想在json formate中返回列表数据。我有两个poco类(Order,Items)使用这些poco类我希望将数据重新调整为json格式

示例Json使用webapi格式化我要返回的内容。

        {"order":{
          "LocationId":1,
           "Amount":"7.79",
          "OrderContactEmail":"test@gmail.com",
          "OrderContactName":"test",
        "items":[{"Options":"y",
       "UnitCost":"7.79",
       "Quantity":"1","MenuItemId":"68"}],
       "DeviceIdentifier":"000000000000000",
        "ShipMethod":"PICK UP",
       "PickupDate":"2011-11-22 15:52:00",
       "OrderContactPhone":"123456"},
        "items":[{"Options":"y",
        "UnitCost":"7.79",
        "Quantity":"1","MenuItemId":"68"}],
        "DeviceIdentifier":"000000000000000",
         "ShipMethod":"PICK UP",
         "PickupDate":"2011-11-22 15:52:00",
         "OrderContactPhone":"123456"}}

1 个答案:

答案 0 :(得分:2)

将您想要的JSON粘贴到http://json2csharp.com中,您将获得此信息:

public class Item
{
    public string Options { get; set; }
    public string UnitCost { get; set; }
    public string Quantity { get; set; }
    public string MenuItemId { get; set; }
}

public class Order
{
    public int LocationId { get; set; }
    public string Amount { get; set; }
    public string OrderContactEmail { get; set; }
    public string OrderContactName { get; set; }
    public Item[] items { get; set; }
    public string DeviceIdentifier { get; set; }
    public string ShipMethod { get; set; }
    public string PickupDate { get; set; }
    public string OrderContactPhone { get; set; }
}

public class Item2
{
    public string Options { get; set; }
    public string UnitCost { get; set; }
    public string Quantity { get; set; }
    public string MenuItemId { get; set; }
}

public class RootObject
{
    public Order order { get; set; }
    public Item2[] items { get; set; }
    public string DeviceIdentifier { get; set; }
    public string ShipMethod { get; set; }
    public string PickupDate { get; set; }
    public string OrderContactPhone { get; set; }
}

这应该指明你的方向......

你也可以这样使用一些匿名类型:

var items = new Item[] {
    item1,
    item2
}

var json = new {
    order = new {
            LocationId = 1
            Items = items
        }
}

等。等