2011-11-24 81 views
0

发布使用嵌套的ArrayList JSON数据我想返回JSON formate.I列表数据有使用这些POCO类两个POCO类(订单,项目),我想retun的数据转换成JSON格式我怎样才能使用的WebAPI

示例Json合成我想要使用webapi返回的内容。

 {"order":{ 
      "LocationId":1, 
      "Amount":"7.79", 
      "OrderContactEmail":"[email protected]", 
      "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"}} 

回答

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 
     } 
} 

等等等

+0

如何在数组中添加项目? – Venkateswararao

+0

我更新了我的示例 –