MVC中Json的使用:Controller中Json的处理

一、当查询得到的数据符合前台要求,不需要做任何处理,直接DataList To Json 返回前台。
代码:

 

1  var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc  Descending", pageSize, searchModel.PageIndex + 1, out recordCount);

2  return Json(allEntities, JsonRequestBehavior.AllowGet);

 

 

 

前台得到的Json数据(两条记录)

 

 1 [

 2     {

 3         "DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca",

 4         "DocumentFileName": "189017.docx.pdf",

 5         "ContentType": "doc",

 6         "Size": 167228,

 7         "InsertedDateUtc": "/Date(1358762613167)/",

 8         "LastModifiedOnDataSource": "/Date(1358504490000)/",

 9         "UniqueIDOnDataSource": "189017",

10         "IsActive": true,

11         "HasBeenIndexed": true,

12         "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",

13         "HasProcessedForAlerts": false,

14         "Congress": "ESMO-2012",

15         "Authors": "Zi Ming Zhao",

16         "CongressType": "Poster",

17         "DocumentTitle": "立普妥-一级预防中的应用                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ",

18         "EntityState": 2,

19         "EntityKey": {

20             "EntitySetName": "Document",

21             "EntityContainerName": "MVCExampleEntities",

22             "EntityKeyValues": [

23                 {

24                     "Key": "DocumentID",

25                     "Value": "61d09198-198e-403e-89a0-01b98402c8ca"

26                 }

27             ],

28             "IsTemporary": false

29         }

30     },

31     {

32         "DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250",

33         "DocumentFileName": "189153.docx.pdf",

34         "ContentType": "doc",

35         "Size": 136195,

36         "InsertedDateUtc": "/Date(1358762610573)/",

37         "LastModifiedOnDataSource": "/Date(1358778247000)/",

38         "UniqueIDOnDataSource": "189153",

39         "IsActive": true,

40         "HasBeenIndexed": true,

41         "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",

42         "HasProcessedForAlerts": false,

43         "Congress": null,

44         "Authors": null,

45         "CongressType": null,

46         "DocumentTitle": "立普妥-碾碎服用                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ",

47         "EntityState": 2,

48         "EntityKey": {

49             "EntitySetName": "Document",

50             "EntityContainerName": "MVCExampleEntities",

51             "EntityKeyValues": [

52                 {

53                     "Key": "DocumentID",

54                     "Value": "a71ea30c-b544-41fa-b008-77adcf7a0250"

55                 }

56             ],

57             "IsTemporary": false

58         }

59     }

60 ]
View Code

 

二、当得到的数据不能满足前台需求,比如,我希望加入一个键值对存入总记录数,以方便做分页。
 
   两种方法:
       1、拼字符串,注意return 的是Content( ).

代码

 1 var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc  Descending", pageSize, searchModel.PageIndex + 1, out recordCount);

 2           

 3            string json = JsonConvert.SerializeObject(allEntities);

 4            StringBuilder sb = new StringBuilder();

 5            sb.Append("{");

 6            sb.Append("\"total\"");

 7            sb.Append(":280,");

 8            sb.Append("\"rows\"");

 9            sb.Append(":");

10            sb.Append(json);

11            sb.Append("}");

12   return Content(sb.ToString());

13  

14           或者直接用String:   string jsonString = "{'total':280,'rows':" + json + "}";  (此方法用easyui测试时,前台无法解析)。

15            return Content(jsonString);

前台得到的Json数据

 1 {

 2     "total": 280,

 3     "rows": [

 4         {

 5             "$id": "1",

 6             "DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca",

 7             "DocumentFileName": "189017.docx.pdf",

 8             "ContentType": "doc",

 9             "Size": 167228,

10             "InsertedDateUtc": "2013-01-21T18:03:33.167",

11             "LastModifiedOnDataSource": "2013-01-18T18:21:30",

12             "UniqueIDOnDataSource": "189017",

13             "IsActive": true,

14             "HasBeenIndexed": true,

15             "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",

16             "HasProcessedForAlerts": false,

17             "Congress": "ESMO-2012",

18             "Authors": "Zi Ming Zhao",

19             "CongressType": "Poster",

20             "DocumentTitle": "立普妥-一级预防中的应用 ",

21             "EntityKey": {

22                 "$id": "2",

23                 "EntitySetName": "Document",

24                 "EntityContainerName": "MVCExampleEntities",

25                 "EntityKeyValues": [

26                     {

27                         "Key": "DocumentID",

28                         "Type": "System.Guid",

29                         "Value": "61d09198-198e-403e-89a0-01b98402c8ca"

30                     }

31                 ]

32             }

33         },

34         {

35             "$id": "3",

36             "DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250",

37             "DocumentFileName": "189153.docx.pdf",

38             "ContentType": "doc",

39             "Size": 136195,

40             "InsertedDateUtc": "2013-01-21T18:03:30.573",

41             "LastModifiedOnDataSource": "2013-01-21T22:24:07",

42             "UniqueIDOnDataSource": "189153",

43             "IsActive": true,

44             "HasBeenIndexed": true,

45             "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",

46             "HasProcessedForAlerts": false,

47             "Congress": null,

48             "Authors": null,

49             "CongressType": null,

50             "DocumentTitle": "立普妥-碾碎服用 ",

51             "EntityKey": {

52                 "$id": "4",

53                 "EntitySetName": "Document",

54                 "EntityContainerName": "MVCExampleEntities",

55                 "EntityKeyValues": [

56                     {

57                         "Key": "DocumentID",

58                         "Type": "System.Guid",

59                         "Value": "a71ea30c-b544-41fa-b008-77adcf7a0250"

60                     }
View Code

 2、重新创建一个新Json对象,把数据装进去。然后返回前台。

 1  var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc  Descending", pageSize, searchModel.PageIndex + 1, out recordCount);

 2    // 创建JsonResult对象。

 3            JsonResult j = new JsonResult()

 4            {

 5                Data = new

 6                {

 7                    total = recordCount,

 8                    rows = allEntities

 9                }

10            };

11            //以下两个参数可选,前台接收有问题时可加上试试

12            // j.ContentType = "application/json";     

13            //j.ContentEncoding = System.Text.Encoding.UTF8;

14            //以下参数设置是否允许GET请求

15            j.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

16            return j; //此处不能再用 return Json(j, JsonRequestBehavior.AllowGet);//方式,否则相当于把Json又转换了一遍Json,前台接收的数据如下(我用easyui测试时,前台无法解析)。

EasyUi环境下有问题的Json

 1 {

 2     "ContentEncoding": null,

 3     "ContentType": null,

 4     "Data": {

 5         "total": 920,

 6         "rows": [

 7             {

 8                 "DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca",

 9                 "DocumentFileName": "189017.docx.pdf",

10                 "ContentType": "doc",

11                 "Size": 167228,

12                 "InsertedDateUtc": "/Date(1358762613167)/",

13                 "LastModifiedOnDataSource": "/Date(1358504490000)/",

14                 "UniqueIDOnDataSource": "189017",

15                 "IsActive": true,

16                 "HasBeenIndexed": true,

17                 "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",

18                 "HasProcessedForAlerts": false,

19                 "Congress": "ESMO-2012",

20                 "Authors": "Zi Ming Zhao",

21                 "CongressType": "Poster",

22                 "DocumentTitle": "立普妥-一级预防中的应用                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ",

23                 "EntityState": 2,

24                 "EntityKey": {

25                     "EntitySetName": "Document",

26                     "EntityContainerName": "MVCExampleEntities",

27                     "EntityKeyValues": [

28                         {

29                             "Key": "DocumentID",

30                             "Value": "61d09198-198e-403e-89a0-01b98402c8ca"

31                         }

32                     ],

33                     "IsTemporary": false

34                 }

35             },

36             {

37                 "DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250",

38                 "DocumentFileName": "189153.docx.pdf",

39                 "ContentType": "doc",

40                 "Size": 136195,

41                 "InsertedDateUtc": "/Date(1358762610573)/",

42                 "LastModifiedOnDataSource": "/Date(1358778247000)/",

43                 "UniqueIDOnDataSource": "189153",

44                 "IsActive": true,

45                 "HasBeenIndexed": true,

46                 "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",

47                 "HasProcessedForAlerts": false,

48                 "Congress": null,

49                 "Authors": null,

50                 "CongressType": null,

51                 "DocumentTitle": "立普妥-碾碎服用                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ",

52                 "EntityState": 2,

53                 "EntityKey": {

54                     "EntitySetName": "Document",

55                     "EntityContainerName": "MVCExampleEntities",

56                     "EntityKeyValues": [

57                         {

58                             "Key": "DocumentID",

59                             "Value": "a71ea30c-b544-41fa-b008-77adcf7a0250"

60                         }

61                     ],

62                     "IsTemporary": false

63                 }

64             }

65         ]

66     },

67     "JsonRequestBehavior": 0,

68     "MaxJsonLength": null,

69     "RecursionLimit": null

70 }
View Code

三 、最后一个Json数据查看工具,很方便。
JSON Viewer

 

你可能感兴趣的:(controller)