0

I want return the list data in json formate.I have Two poco classes (Order,Items) using these poco classes i want retun the data into json format

Sample Json formate what i want return using 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"}}

1 Answer 1

2

Pasting your wanted JSON into http://json2csharp.com you'll get this:

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

This should point you the direction...

You could also use some anonymous types this way:

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

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

etc. etc.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I append Items in order array ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.