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

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

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

MVC中Json的使用:Controller中Json的处理【转】-LMLPHP
 [
{
"DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca",
"DocumentFileName": "189017.docx.pdf",
"ContentType": "doc",
"Size": 167228,
"InsertedDateUtc": "/Date(1358762613167)/",
"LastModifiedOnDataSource": "/Date(1358504490000)/",
"UniqueIDOnDataSource": "189017",
"IsActive": true,
"HasBeenIndexed": true,
"TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",
"HasProcessedForAlerts": false,
"Congress": "ESMO-2012",
"Authors": "Zi Ming Zhao",
"CongressType": "Poster",
"DocumentTitle": "立普妥-一级预防中的应用 ",
"EntityState": 2,
"EntityKey": {
"EntitySetName": "Document",
"EntityContainerName": "MVCExampleEntities",
"EntityKeyValues": [
{
"Key": "DocumentID",
"Value": "61d09198-198e-403e-89a0-01b98402c8ca"
}
],
"IsTemporary": false
}
},
{
"DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250",
"DocumentFileName": "189153.docx.pdf",
"ContentType": "doc",
"Size": 136195,
"InsertedDateUtc": "/Date(1358762610573)/",
"LastModifiedOnDataSource": "/Date(1358778247000)/",
"UniqueIDOnDataSource": "189153",
"IsActive": true,
"HasBeenIndexed": true,
"TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",
"HasProcessedForAlerts": false,
"Congress": null,
"Authors": null,
"CongressType": null,
"DocumentTitle": "立普妥-碾碎服用 ",
"EntityState": 2,
"EntityKey": {
"EntitySetName": "Document",
"EntityContainerName": "MVCExampleEntities",
"EntityKeyValues": [
{
"Key": "DocumentID",
"Value": "a71ea30c-b544-41fa-b008-77adcf7a0250"
}
],
"IsTemporary": false
}
}
]
MVC中Json的使用:Controller中Json的处理【转】-LMLPHP
二、当得到的数据不能满足前台需求,比如,我希望加入一个键值对存入总记录数,以方便做分页。
 
   两种方法:
       1、拼字符串,注意return 的是Content( ).

代码

MVC中Json的使用:Controller中Json的处理【转】-LMLPHP
 var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc  Descending", pageSize, searchModel.PageIndex + , out recordCount);

            string json = JsonConvert.SerializeObject(allEntities);
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append("\"total\"");
sb.Append(":280,");
sb.Append("\"rows\"");
sb.Append(":");
sb.Append(json);
sb.Append("}");
return Content(sb.ToString()); 或者直接用String: string jsonString = "{'total':280,'rows':" + json + "}"; (此方法用easyui测试时,前台无法解析)。
return Content(jsonString);
MVC中Json的使用:Controller中Json的处理【转】-LMLPHP

前台得到的Json数据

MVC中Json的使用:Controller中Json的处理【转】-LMLPHP
 {
"total": 280,
"rows": [
{
"$id": "1",
"DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca",
"DocumentFileName": "189017.docx.pdf",
"ContentType": "doc",
"Size": 167228,
"InsertedDateUtc": "2013-01-21T18:03:33.167",
"LastModifiedOnDataSource": "2013-01-18T18:21:30",
"UniqueIDOnDataSource": "189017",
"IsActive": true,
"HasBeenIndexed": true,
"TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",
"HasProcessedForAlerts": false,
"Congress": "ESMO-2012",
"Authors": "Zi Ming Zhao",
"CongressType": "Poster",
"DocumentTitle": "立普妥-一级预防中的应用 ",
"EntityKey": {
"$id": "2",
"EntitySetName": "Document",
"EntityContainerName": "MVCExampleEntities",
"EntityKeyValues": [
{
"Key": "DocumentID",
"Type": "System.Guid",
"Value": "61d09198-198e-403e-89a0-01b98402c8ca"
}
]
}
},
{
"$id": "3",
"DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250",
"DocumentFileName": "189153.docx.pdf",
"ContentType": "doc",
"Size": 136195,
"InsertedDateUtc": "2013-01-21T18:03:30.573",
"LastModifiedOnDataSource": "2013-01-21T22:24:07",
"UniqueIDOnDataSource": "189153",
"IsActive": true,
"HasBeenIndexed": true,
"TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",
"HasProcessedForAlerts": false,
"Congress": null,
"Authors": null,
"CongressType": null,
"DocumentTitle": "立普妥-碾碎服用 ",
"EntityKey": {
"$id": "4",
"EntitySetName": "Document",
"EntityContainerName": "MVCExampleEntities",
"EntityKeyValues": [
{
"Key": "DocumentID",
"Type": "System.Guid",
"Value": "a71ea30c-b544-41fa-b008-77adcf7a0250"
}
MVC中Json的使用:Controller中Json的处理【转】-LMLPHP

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

MVC中Json的使用:Controller中Json的处理【转】-LMLPHP
  var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc  Descending", pageSize, searchModel.PageIndex + , out recordCount);
// 创建JsonResult对象。
JsonResult j = new JsonResult()
{
Data = new
{
total = recordCount,
rows = allEntities
}
};
//以下两个参数可选,前台接收有问题时可加上试试
// j.ContentType = "application/json";
//j.ContentEncoding = System.Text.Encoding.UTF8;
//以下参数设置是否允许GET请求
j.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return j; //此处不能再用 return Json(j, JsonRequestBehavior.AllowGet);//方式,否则相当于把Json又转换了一遍Json,前台接收的数据如下(我用easyui测试时,前台无法解析)。
MVC中Json的使用:Controller中Json的处理【转】-LMLPHP

EasyUi环境下有问题的Json

MVC中Json的使用:Controller中Json的处理【转】-LMLPHP
 {
"ContentEncoding": null,
"ContentType": null,
"Data": {
"total": 920,
"rows": [
{
"DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca",
"DocumentFileName": "189017.docx.pdf",
"ContentType": "doc",
"Size": 167228,
"InsertedDateUtc": "/Date(1358762613167)/",
"LastModifiedOnDataSource": "/Date(1358504490000)/",
"UniqueIDOnDataSource": "189017",
"IsActive": true,
"HasBeenIndexed": true,
"TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",
"HasProcessedForAlerts": false,
"Congress": "ESMO-2012",
"Authors": "Zi Ming Zhao",
"CongressType": "Poster",
"DocumentTitle": "立普妥-一级预防中的应用 ",
"EntityState": 2,
"EntityKey": {
"EntitySetName": "Document",
"EntityContainerName": "MVCExampleEntities",
"EntityKeyValues": [
{
"Key": "DocumentID",
"Value": "61d09198-198e-403e-89a0-01b98402c8ca"
}
],
"IsTemporary": false
}
},
{
"DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250",
"DocumentFileName": "189153.docx.pdf",
"ContentType": "doc",
"Size": 136195,
"InsertedDateUtc": "/Date(1358762610573)/",
"LastModifiedOnDataSource": "/Date(1358778247000)/",
"UniqueIDOnDataSource": "189153",
"IsActive": true,
"HasBeenIndexed": true,
"TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5",
"HasProcessedForAlerts": false,
"Congress": null,
"Authors": null,
"CongressType": null,
"DocumentTitle": "立普妥-碾碎服用 ",
"EntityState": 2,
"EntityKey": {
"EntitySetName": "Document",
"EntityContainerName": "MVCExampleEntities",
"EntityKeyValues": [
{
"Key": "DocumentID",
"Value": "a71ea30c-b544-41fa-b008-77adcf7a0250"
}
],
"IsTemporary": false
}
}
]
},
"JsonRequestBehavior": 0,
"MaxJsonLength": null,
"RecursionLimit": null
}
MVC中Json的使用:Controller中Json的处理【转】-LMLPHP

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

05-08 08:30