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

本文关键字:JSON 格式 转换 对象 列表 | 更新日期: 2023-09-27 18:20:06

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

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

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

在同一嵌套级别(在您的情况下为级别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"
        }]
    }
}]