一、当查询得到的数据符合前台要求,不需要做任何处理,直接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. jquery $ dollar符号用法总结

    参考:https://github.com/chyingp/blog/blob/master/jquery/jQuery%E6%BA%90%E7%A0%81-%E7%BE%8E%E5%85%83$%E ...

  2. haproxy hdr和path

    path : string This extracts the request's URL path, which starts at the first slash and ends before ...

  3. html类,id规范命名

    DIV+CSS的命名规则 SEO(搜索引擎优化)有很多工作要做,其中对代码的优化是一个很关键的步骤.为了更加符合SEO的规范,下面中部IT网将对目前流行的CSS+DIV的命名规则整理如下: 页头:he ...

  4. gcc选项-g与-rdynamic的异同

    摘自http://www.tuicool.com/articles/EvIzUn gcc选项-g与-rdynamic的异同 gcc 的 -g ,应该没有人不知道它是一个调试选项,因此在一般需要进行程序 ...

  5. mac系统奔溃无法启动时,如何备份重要资料

    虽然说苹果系统以稳定性获得高度好评,但是作为一名程序员,还是要考虑到系统奔溃的情况. 当遇到系统奔溃,无法启动,而我们还没有备份电脑里面的重要资料,这时候不用着急.可以用下面的 方法来拯救你的苹果电脑 ...

  6. 使用VS2003 发送Email

    使用VS2003发送Email与之后VS2005版本及以上VS版本不一样,记录一下, 需要引用using System.Web.Mail; public void SendEmail() { try ...

  7. tomcat 发布webService

    <!-- tomcat发布webservice时所需jar --> <dependency> <groupId>com.sun.xml.ws</groupId ...

  8. Object-C 重载

    方法重载要保证三个条件 1在同一个类中 2.方法参数类型相同 名称相同 3.方法的参数不同 请看下面的例子 @interface whgMyObject : NSObject -(void)print ...

  9. ASP.Net引用类库出现问题 二

    一:引用mysql.data.dll出现,问题? error: Package MySql.Data (.NETCoreApp,Version=v1.). Package MySql.Data sup ...

  10. 转:HTML 5 控件事件属性

    Window 事件属性 window 对象触发的事件. 适用于 <body> 标签: 属性 值 描述 onafterprint script 在打印文档之后运行脚本 onbeforepri ...