前后台传值一般情况下,都会用到json类型的数据,比较常见,但是每次用到的时候去网上找比较麻烦,所以自己记录一下,下次直接用。

构造的json串格式,如下:

[{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"},
{"id":"","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}]

一、构造DataTable

      public DataTable getData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(Int32));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("active", typeof(string));
dt.Columns.Add("user_id", typeof(string));
dt.Columns.Add("no_of_reports", typeof(string));
for (int i = ; i < ; i++)
{
dt.Rows.Add(i, "name", "active", "user_id", "no_of_reports");
}
return dt;
}

二、DataTable转json

     public string DataTableToJsonWithStringBuilder(DataTable table)
{
var jsonString = new StringBuilder();
if (table.Rows.Count > )
{
jsonString.Append("[");
for (int i = ; i < table.Rows.Count; i++)
{
jsonString.Append("{");
for (int j = ; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - )
{
jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - )
{
jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - )
{
jsonString.Append("}");
}
else
{
jsonString.Append("},");
}
}
jsonString.Append("]");
}
return jsonString.ToString();
}

三、调用

  DataTable dt = getData();
string str = DataTableToJsonWithStringBuilder(dt);

C# 后台构造json数据的更多相关文章

  1. 传递给后台的Json数据解析

    后台代码如下: public void ProcessRequest(HttpContext context) { context.Response.ContentType = "appli ...

  2. jquery用ajax方式从后台获取json数据,将内容填充到下拉列表。

    从后台获取json数据,将内容填充到下拉列表. url:链接 par:ID sel:下拉列表选择器 //获取下拉列表 function BuildSelectBox(url, par, sel) { ...

  3. Jquery Ajax和getJSON获取后台普通Json数据和层级Json数据解析

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

  4. 反面教材 构造构造 json 数据

    构造构造 json 数据 说说你们在项目中遇到过的最糟糕的代码 - V2EX https://www.v2ex.com/t/214099

  5. jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表

    对于问题从后台获取json数据,将内容填充到下拉列表,代码非常简单,具体过程请看下面代码. 需求:url:链接     par:ID       sel:下拉列表选择器 function BuildS ...

  6. easyUI-combobox 后台导入Json数据的方法

    一.前台页面: <input id="List" class="easyui-combobox" data-options="valueFiel ...

  7. 通过ajax和spring 后台传输json数据

    在通过ajax从页面向后台传数据的时候,总是返回415(Unsupported media type)错误,后台无法获取数据.如下图所示: 在尝试解决这个问题的时候,我们首先要理解一下概念: @req ...

  8. 前台js接收后台的json数据

    后台返回的json数据,如php的: return json_encode($data); 在前台 js接收如下: function json2object(str){ var jsstr = str ...

  9. 微信小程序,请求php后台返回json数据多出隐藏字符问题

    这几天在做一个微信小程序注册登录页面的时候碰到一个问题,就是使用wx.request api的时候success中返回的JSON数据前面会多出空白字符,后面网上查了一下是说php bom头问题(详细介 ...

随机推荐

  1. 性能优化之reflow和repaint

    本文主要介绍一下什么是reflow,repaint, 怎样避免它们造成的不良影响, 怎么通过工具查看分析它们. 一.首先对浏览器渲染引擎下网页呈现过程简要说一下: 浏览器的渲染引擎开始解析html构建 ...

  2. form + iframe 获取表单提交后返回的数据

    原理: submit 提交表单没有回调函数,但是可以用iframe来接收返回结果,最后进行格式转换就ok了: 原文地址: http://blog.csdn.net/simeng_1016/articl ...

  3. Spring依赖注入 — util命名空间配置

    要使用<util>标签,必须在XML中加入util名称空间(namespace): xmlns:util="http://http://www.springframework.o ...

  4. Java 类文件结构

    Java 诞生之时有句著名的宣传口号"Write Once, Run Anywhere.".但是,Java 语言本身不具备跨平台的能力,而是 JVM 提供了跨平台的能力. 事实上, ...

  5. 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用

    zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...

  6. 初探云服务器ECS(Linux系统)

    PS: 购买的阿里云服务器(ECS,Linux系统),使用的弹性公网IP(EIP). 一.使用Xshell链接ECS 1.将公网IP填入主机即可 2.用户名一般为root,密码是自己设置的,填入即可. ...

  7. Axis1.4之定制发布服务

    将axis1.4_home\webapps目录下的axis文件夹拷贝到tomcat_home\webapps目录下.然后在tomcat_home\webapps\axis\WEB-INF\lib下添加 ...

  8. reinterpret_cast,static_cast, dynamic_cast,const_cast的运用分析

    reinterpret_cast(重新解释类型转换) reinterpret_cast 最famous的特性就是什么都可以,转换任意的类型,包括C++所有通用类型,所以也最不安全 应用 整形和指针之间 ...

  9. STL --> map容器

    map容器 一.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 二.ma ...

  10. BLESS学习笔记

    BLESS全称:Bloom-filter-based Error Correction Solution for High-throughput Sequencing Reads,即基于布隆过滤器的高 ...