asp.net core有时候需要在视图中将c#对象序列化为json格式,比如在使用ajax时。
var dataModel = @Json.Serialize(Model); 如上图所示,使用Json.Serialize就可以序列化对象,然后可以将其传递给javascript或者进行其它操作。序列后的对象如下所示:
var dataModel = { "groupId": "6448dbf2-b96a-4262-bd1f-f21a8d6c2d7d", "groupName": "站长组", "authorizeModels": [{ "authorizeType": 0, "enable": true }, { "authorizeType": 50, "enable": true }, { "authorizeType": 1000, "enable": true }, { "authorizeType": 101, "enable": true }, { "authorizeType": 1004, "enable": true }, { "authorizeType": 1009, "enable": true }, { "authorizeType": 1004, "enable": true }, { "authorizeType": 1005, "enable": true }, { "authorizeType": 1006, "enable": true }, { "authorizeType": 1009, "enable": true }, { "authorizeType": 1100, "enable": true }, { "authorizeType": 1901, "enable": true }, { "authorizeType": 2001, "enable": true }, { "authorizeType": 2002, "enable": true }, { "authorizeType": 2003, "enable": true }, { "authorizeType": 2004, "enable": true }, { "authorizeType": 2005, "enable": true }, { "authorizeType": 2006, "enable": true }, { "authorizeType": 2007, "enable": true }, { "authorizeType": 2100, "enable": true }, { "authorizeType": 2901, "enable": true }, { "authorizeType": 3001, "enable": true }, { "authorizeType": 3002, "enable": true }, { "authorizeType": 3003, "enable": true }, { "authorizeType": 3004, "enable": true }, { "authorizeType": 3003, "enable": true }, { "authorizeType": 3901, "enable": true }], "errorMessage": null };