前后台传值一般情况下,都会用到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. 百度和谷歌的逆地址解析及GPS、谷歌地图和百度地图坐标之间的转换(python版)

    #!/usr/bin/env python # coding:utf-8 # @author: KaiVen """ GPS坐标转换: WGS-84:是国际标准,GPS坐 ...

  2. bootstrap 栅格系统 自动隐藏

    1 Container 顾名思义Container是栅格系统最外层的class,直接被container包裹的只能是row这个class.需要注意的是container自带左右各15px paddin ...

  3. install-scp

    centos6 minilize system will not scp command install: yum -y install openssh-clients and another mac ...

  4. 常用的Oracle函数收集

    to_char(); count(); avg(); sum(); to_date('时间','格式'); NVL(,); NVL2(); substr(); case   when  then   ...

  5. laravel-Policy步骤

    用户授权Policy 定义策略类 php artisan make:policy <name> 定义方法 注册策略类和模型关联 app > Providers > AuthSe ...

  6. http.request的请求

    var http=require('http'); var request=require('request'); var body = { "data":{ "id&q ...

  7. 【Python】 迭代器&生成器

    迭代器 任何一个类,只要其实现了__iter__方法,就算是一个可迭代对象.可迭代对象的__iter__方法返回的对象是迭代器,迭代器类需要实现next方法.一般来说,实现了__iter__方法的类肯 ...

  8. java开源安全框架-------Apache Shiro--第二天

    身份验证 即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标志信息来表明他就是他本人,如提供身份证.用户名.密码来证明 在shiro中,用户需要提供principals(身份)和crede ...

  9. (Matlab)GPU计算及CPU计算能力的比较

    %%首先以200*200的矩阵做加减乘除 做比较 t = zeros(1,100); A = rand(200,200);B = rand(200,200);C = rand(200,200); fo ...

  10. java错题集

    解析:java中,JavaDoc注释以 /** 开头(中间写内容)以*/结尾 解析:A对象是类的实例,不是集合,其余正确 解析:创建一个对象的语法为: 类名 对象名=new 类名();,因此正确答案为 ...