一、当查询得到的数据符合前台要求,不需要做任何处理,直接DataList To 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
}
}
]
二、当得到的数据不能满足前台需求,比如,我希望加入一个键值对存入总记录数,以方便做分页。
 
   两种方法:
       1、拼字符串,注意return 的是Content( ).

代码

 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的处理的更多相关文章

  1. spring mvc中的service和controller中读取不到properties值

    根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式

  2. 控制器controller与指令中的link、controller中变量作用域的关系

    angjualrjs中的作用域与原生js中的函数嵌套原理一致,都是存在作用域的继承.若在子控制器(同样包括在指令中的link或是controllerding中定义变量,此时指令中必须未使用scope独 ...

  3. ASP.NET MVC 前端(View)向后端(Controller)中传值

    在MVC中,要把前端View中的值传递给后端Controller, 主要有两种方法 1. 利用Request.Form 或者 Request.QueryString public ActionResu ...

  4. spring mvc Controller中使用@Value无法获取属性值

    在使用spring mvc时,实际上是两个spring容器: 1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件 org.sp ...

  5. spring controller中@Value取不到applicationContext.xml中加载配置文件的问题

    原因还未查证: http://sunjun041640.blog.163.com/blog/static/256268322014127113844746/ 在使用spring mvc时,实际上是两个 ...

  6. 在Controller中添加事务管理

    文章参考了此博客: https://blog.csdn.net/qq_40594137/article/details/82772545 写这篇文章之前先说明一下: 1. Controller中添加事 ...

  7. ASP.NET MVC 学习1、新增Controller,了解MVC运行机制

    1,turorial ,根据链接教程新建一个MVC项目 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  8. 在MVC中使用Json.Net序列化和反序列化Json对象

    在.Net的MVC开发中,经常会使用到Json对象,于是,系统提供了JsonResult这个对象,其本质是调用.Net系统自带的Json序列化类JavaScriptSerializer对数据对象进行序 ...

  9. 在jfinal的Controller中接受json数据

    JFinal中接收URL中的参数或者model中的参数是很方便的,但是对于web2.0的网站来说,经常会以json方式提交比较复杂的数据,比如一个查询,包含了各种过滤条件和排序分页,前端脚本可能提交的 ...

  10. Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法

    之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...

随机推荐

  1. 关于RPC与MQ异同的理解

    最近看了一些资料,回顾过去项目的经验,梳理自己对两者异同的理解: 相同: 1.都利于大型系统的解耦: 2.都提供子系统之间的交互,特别是异构子系统(如java\node等不同开发语言): 不同: 1. ...

  2. 前端自动化测试工具doh学习总结(二)

    一.robot简介 robot是dojo框架中用来进行前端自动化测试的工具,doh主要目的在于单元测试,而robot可以用来模仿用户操作来测试UI.总所周知,Selenium也是一款比较流行的前端自动 ...

  3. channel vs mutex

    记录许总演讲PPT指出的实践: channel– 本质上是一个 MessageQueue– 非常正统的执行体间通讯设施• sync.Mutex/RWMutex/Cond/etc– 不要把 channe ...

  4. [异常解决] Keil安装好nRF51822开发环境,运行DEMO报错:Error:“GPIOTE_CONFIG_NUM_OF_LOW_POWER_ENVENTS” is undefined

    1.问题描述 when compiling "ble_app_proximity" exampled by Nordic, it indicates errors: "D ...

  5. js 倒计时实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 《OOC》笔记(0)——为何要看这本书

    <OOC>笔记(0)——为何要看这本书 <OOC>全名是<Object-oriented Programming with ANSI-C>,作者Axel-Tobia ...

  7. 网络误区:不用中间变量交换2个变量的value,最高效的是异或运算.

    本文记录了不使用中间变量交换2个变量的value,很多的网络留言说是直接异或运算就可以了,而且效率很高,是真的吗? 这里简单的说一下我的环境:Win7 32位,Qt creator 5.4.1 编译器 ...

  8. Android Activity的生命周期简单总结

    Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...

  9. osgi 2

    基础的API BundleActivator  BundleContext ServiceReference HelloServiceFactory ServiceTracker osgi 疑惑: I ...

  10. Atitit 异常机制与异常处理的原理与概论

    Atitit 异常机制与异常处理的原理与概论 1. 异常vs 返回码1 1.1. 返回码模式的处理 (瀑布if 跳到失败1 1.2. 终止模式  vs 恢复模式(asp2 1.3. 异常机制的设计原理 ...