将object格式转为json格式

在页面内容显示时,有时需要用到json格式。但数据库内容的显示,需要将数据库中获取的格式转为json:

using Newtonsoft.Json;
public
static string ToJson(object obj) { return JsonConvert.SerializeObject(obj); } public static string ToJson(object obj, int records) { string json = ToJson(obj); return @"{""total"":" + records.ToString() + @",""rows"":" + json + "}"; }

第一段代码是直接将object格式转为json格式;第二段是在转json的基础上,加上total总数。

上述代码一般定义在.Common项目中的JsonHelper中。

posted @ 2016-08-12 15:32  JennyJiang  阅读(1531)  评论(0编辑  收藏  举报