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()方法 所有都是自己写的, ...
随机推荐
- C# 弗洛伊德(Floyd)算法
弗洛伊德(Floyd)算法 主要是用于计算图中所有顶点对之间的最短距离长度的算法,如果是要求某一个特定点到图中所有顶点之间的最短距离可以用; ; ; ; ...
- nginx grok 正则错误的输出情况
nginx 配置: http { include mime.types; default_type application/octet-stream; log_format main '$http_h ...
- C++类对应的内存结构
提示1:对“内存结构”表示有疑问或不解的,先参考: http://blog.csdn.net/guogangj/archive/2007/05/25/1625199.aspx, 本文使用的表示方法和V ...
- 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...
- 可执行文件(ELF)格式的理解
摘自http://www.cnblogs.com/xmphoenix/archive/2011/10/23/2221879.html 可执行文件(ELF)格式的理解 ELF(Executable an ...
- 使用ashx一般处理程序,读取不到Session的问题
一般的处理程序文件里面是用不了Session的,必须得实现Session接口才可以用. public class RandomCode : IHttpHandler, System.Web.Sessi ...
- cf #214div2
Dima and Guards Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- Qt之再谈窗体阴影
前面就窗口阴影已经写过一篇博客,使用九宫格的思路实现的,在我看来,凡是用程序能实现的尽量不要使用图片代替(在保证效率的前提下),今天再次分享关于我的一些小见解! 先看效果: 窗口阴 ...
- Spark里面的任务调度:离SparkContext开始
SparkContext这是发达国家Spark入学申请,它负责的相互作用和整个集群,它涉及到创建RDD.accumulators and broadcast variables.理解力Spark架构, ...
- css float笔记
float 1.破坏性,让设置了float属性的元素脱离文档流 2.包裹性:div设置了float之后,其宽度会自动调整为包裹住内容宽度,而不是撑满整个父容器.如果没有包裹性,如何实现文字环绕(flo ...