MVC中Json的使用:Controller中Json的处理【转】
var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc Descending", pageSize, searchModel.PageIndex + , out recordCount);
return Json(allEntities, JsonRequestBehavior.AllowGet);
前台得到的Json数据(两条记录)

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

代码

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

前台得到的Json数据

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

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

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测试时,前台无法解析)。

EasyUi环境下有问题的Json

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

三 、最后一个Json数据查看工具,很方便。
JSON Viewer
MVC中Json的使用:Controller中Json的处理【转】的更多相关文章
- spring mvc中的service和controller中读取不到properties值
根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式
- 控制器controller与指令中的link、controller中变量作用域的关系
angjualrjs中的作用域与原生js中的函数嵌套原理一致,都是存在作用域的继承.若在子控制器(同样包括在指令中的link或是controllerding中定义变量,此时指令中必须未使用scope独 ...
- ASP.NET MVC 前端(View)向后端(Controller)中传值
在MVC中,要把前端View中的值传递给后端Controller, 主要有两种方法 1. 利用Request.Form 或者 Request.QueryString public ActionResu ...
- spring mvc Controller中使用@Value无法获取属性值
在使用spring mvc时,实际上是两个spring容器: 1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件 org.sp ...
- spring controller中@Value取不到applicationContext.xml中加载配置文件的问题
原因还未查证: http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/ 在使用spring mvc时,实际上是两个 ...
- 在Controller中添加事务管理
文章参考了此博客: https://blog.csdn.net/qq_40594137/article/details/82772545 写这篇文章之前先说明一下: 1. Controller中添加事 ...
- ASP.NET MVC 学习1、新增Controller,了解MVC运行机制
1,turorial ,根据链接教程新建一个MVC项目 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...
- 在MVC中使用Json.Net序列化和反序列化Json对象
在.Net的MVC开发中,经常会使用到Json对象,于是,系统提供了JsonResult这个对象,其本质是调用.Net系统自带的Json序列化类JavaScriptSerializer对数据对象进行序 ...
- 在jfinal的Controller中接受json数据
JFinal中接收URL中的参数或者model中的参数是很方便的,但是对于web2.0的网站来说,经常会以json方式提交比较复杂的数据,比如一个查询,包含了各种过滤条件和排序分页,前端脚本可能提交的 ...
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...
随机推荐
- 将数组适配到ListView
public class TutListActivity extends ListActivity { @Override public void onCreate(Bundle savedInsta ...
- ARM和X86功耗差别的深层原因探讨
ARM和X86功耗差别的深层原因探讨 ARM和X86功耗的差别一直是个很热的话题.ARM可以做的很低,甚至1瓦都不到.而X86服务器的芯片可以达到100-200瓦,就算是嵌入式处理器Atom系列也需要 ...
- 【POJ 1182 食物链】并查集
此题按照<挑战程序设计竞赛(第2版)>P89的解法,不容易想到,但想清楚了代码还是比较直观的. 并查集模板(包含了记录高度的rank数组和查询时状态压缩) *; int par[MAX_N ...
- Unity doesn't load, no Launcher, no Dash appears
1. 重新安装 ubuntu-desktop不起作用. Enter the following commands:- Ctrl+Alt+F1 login there by user name and ...
- 基于Andoird 4.2.2的同步框架源代码学习——同步提供端
Android同步框架 同步(synchronization)允许用户将远程数据下载到新的设备上,同时将设备上的帐户数据上传到远端.同步还保证用户能够看到最新的数据. 开发者自然可以通过自己的方式来设 ...
- 在SQL Server中如何快速查找DBCC命令和语法?
DBCC命令非常好用,但是命令很多语法就很多,如何快速记忆呢?是否都要背下来.其实不用,只要能知道每个命令的作用并且记住DBCC HELP命令就可以了. --查找所有的DBCC命令 DBCC HEL ...
- Stopwatch 和TimeSpan介绍【转】
1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...
- mvc3.0防止跨站点请求伪造(CSRF)攻击
众所周知,asp.net mvc程序在浏览器运行是产生标准的Html标签,包括浏览器要发送的关键数据等内容都在html内容里面.听起来不错,但是假如我们伪造类似的html内容,更改里面的关键数据,在浏 ...
- netty demo
Netty 4.0 demo netty是一个异步,事件驱动的网络编程框架&工具,使用netty,可以快速开发从可维护,高性能的协议服务和客户端应用.是一个继mina之后,一个非常受欢迎的 ...
- HTML5 canvas 绘制精美的图形
HTML5 是一个新兴标准,它正在以越来越快的速度替代久经考验的 HTML4.HTML5 是一个 W3C “工作草案” — 意味着它仍然处于开发阶段 — 它包含丰富的元素和属性,它们都支持现行的 HT ...