如何将对象列表转换为正确的JSON格式?

时间:2013-06-26 19:48:09

标签: c# json asp.net-mvc-3 linq-to-entities

我运行一个查询,返回客户订单列表:

SELECT cust_no, cust_name, order_no, order_name FROM CustomerOrders

在我的Controller Action方法中,我执行以下操作(_context是我的DataContext):

var results = _context.CustomerOrders.ToList();

return Json(results, JsonRequestBehavior.AllowGet);

当我在调试器中检查它时,我看到了对象列表,但是对Json不太熟悉,我不确定当它作为Json字符串时它会是什么样子。我想要的格式是:

{
    "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"
            }
        ]
    }
}

我目前可以进入这个:

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

使用:

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

1 个答案:

答案 0 :(得分: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"
        }]
    }
}]