0

I run a query that returns me a list of customers orders:

SELECT cust_no, cust_name, order_no, order_name FROM CustomerOrders

In my Controller Action method, I do the following (_context is my DataContext):

var results = _context.CustomerOrders.ToList();

return Json(results, JsonRequestBehavior.AllowGet);

When I inspect this in the debugger, I see the list of objects, but not being too familiar with Json, I am not sure how this would look when it as a Json string. The format I want is:

{
    "Customer": {
        "cust_no": "123",
        "cust_name": "john",
        "Orders": [
            {
                "order_no": "1",
                "order_name": "order1"
            },
            {
                "order_no": "2",
                "order_name": "order2"
            }
        ]
     },
     "Customer": {
         "cust_no": "456",
         "cust_name": "jane",
         "Orders": [
            {
                "order_no": "3",
                "order_name": "order3"
            },
            {
                "order_no": "4",
                "order_name": "order4"
            }
        ]
    }
}

I currently can get into this:

{ Customer = "123", cust_name = "John", Orders = "1", oder_no = "order1" }

with:

_context.CustomerOrders.Select(x => new 
                                    {
                                        Customer= x.cust_no, x.cust_name, 
                                        Orders = x.order_no, x.order_name});

public ActionResult GetCustomerOrders()
    {
        JsonResult result = null;
        try
        {
            var results = new {Customers = _context.CustomerOrders.ToList()};

            return Json(results,JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            //Log
        }

        return result;
    }
4

1 回答 1

2

您不能在同一嵌套级别(在您的情况下为级别 0)拥有具有相同键的 JSON 键值对。如果需要,可以通过执行以下操作返回匿名类型:

var results = new {Customers = _context.CustomerOrders.ToList()};

注意- 忘了补充上面的代码看起来像:

{
    Customers: [{
        "cust_no": "123",
            "cust_name": "john",
            "Orders": [{
            "order_no": "1",
                "order_name": "order1"
        }, {
            "order_no": "2",
                "order_name": "order2"
        }]
    }, {
        "cust_no": "456",
            "cust_name": "jane",
            "Orders": [{
            "order_no": "3",
                "order_name": "order3"
        }, {
            "order_no": "4",
                "order_name": "order4"
        }]
    }]
}

在此处查看 JSON 规范:http: //www.json.org/

编辑 - (响应修复您的 Linq 查询) 以下是您需要的:

    var results = (from co in _context.CustomerOrders
                   group co by new { co.cust_no, co.cust_name } into orders
                   select new
                   {
                       Customer = new
                       {
                           cust_name = orders.Key.cust_name,
                           cust_no = orders.Key.cust_no,
                           Orders = orders.Select(o=> new{o.order_name,o.order_no })
                       }
                   }).AsEnumerable();
    return Json(results, JsonRequestBehavior.AllowGet);

使用模拟数据的结果应如下所示:

[{
    "Customer": {
        "cust_name": "ted",
        "cust_no": "1441865486",
        "Orders": [{
            "order_name": "ted1271196444",
            "order_no": "1877898370"
        }, {
            "order_name": "ted1137404580",
            "order_no": "1033969821"
        }, {
            "order_name": "ted113580415",
            "order_no": "844051358"
        }, {
            "order_name": "ted842422359",
            "order_no": "1063097922"
        }, {
            "order_name": "ted2140579126",
            "order_no": "1170215299"
        }, {
            "order_name": "ted843928549",
            "order_no": "2143378901"
        }]
    }
}, {
    "Customer": {
        "cust_name": "Jack",
        "cust_no": "1258770771",
        "Orders": [{
            "order_name": "Jack879867938",
            "order_no": "585569719"
        }, {
            "order_name": "Jack1423388998",
            "order_no": "209013154"
        }]
    }
}, {
    "Customer": {
        "cust_name": "joe",
        "cust_no": "1223478754",
        "Orders": [{
            "order_name": "joe1283306017",
            "order_no": "1305330220"
        }, {
            "order_name": "joe1369830458",
            "order_no": "1996259538"
        }, {
            "order_name": "joe1772918032",
            "order_no": "1265675292"
        }, {
            "order_name": "joe535974281",
            "order_no": "837890619"
        }, {
            "order_name": "joe194556914",
            "order_no": "812224857"
        }, {
            "order_name": "joe28812423",
            "order_no": "515669909"
        }, {
            "order_name": "joe2004245093",
            "order_no": "1634742463"
        }]
    }
}, {
    "Customer": {
        "cust_name": "jill",
        "cust_no": "659748358",
        "Orders": [{
            "order_name": "jill1462582377",
            "order_no": "1817173079"
        }, {
            "order_name": "jill1848627650",
            "order_no": "830495643"
        }, {
            "order_name": "jill727215465",
            "order_no": "728808273"
        }, {
            "order_name": "jill1071911623",
            "order_no": "824043403"
        }, {
            "order_name": "jill126843849",
            "order_no": "1654825240"
        }]
    }
}]
于 2013-06-26T19:55:47.653 回答