【问题标题】:How can i Post the json data with Nested arraylist using webapi我如何使用 webapi 使用嵌套数组列表发布 json 数据
【发布时间】:2011-11-25 15:49:52
【问题描述】:

我想以 json 格式返回列表数据。我有两个使用这些 poco 类的 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"}}

【问题讨论】:

    标签: json.net wcf-web-api


    【解决方案1】:

    将你想要的 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
            }
    }
    

    等等。等等

    【讨论】:

    • 我怎样才能在订单数组中追加项目?
    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 2021-08-22
    • 2020-06-02
    • 1970-01-01
    • 2020-05-06
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多