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

构造的json串格式,如下:

  1. [{"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

  1. public DataTable getData()
  2. {
  3. DataTable dt = new DataTable();
  4. dt.Columns.Add("id", typeof(Int32));
  5. dt.Columns.Add("name", typeof(string));
  6. dt.Columns.Add("active", typeof(string));
  7. dt.Columns.Add("user_id", typeof(string));
  8. dt.Columns.Add("no_of_reports", typeof(string));
  9. for (int i = ; i < ; i++)
  10. {
  11. dt.Rows.Add(i, "name", "active", "user_id", "no_of_reports");
  12. }
  13. return dt;
  14. }

二、DataTable转json

  1. public string DataTableToJsonWithStringBuilder(DataTable table)
  2. {
  3. var jsonString = new StringBuilder();
  4. if (table.Rows.Count > )
  5. {
  6. jsonString.Append("[");
  7. for (int i = ; i < table.Rows.Count; i++)
  8. {
  9. jsonString.Append("{");
  10. for (int j = ; j < table.Columns.Count; j++)
  11. {
  12. if (j < table.Columns.Count - )
  13. {
  14. jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
  15. + "\":" + "\""
  16. + table.Rows[i][j].ToString() + "\",");
  17. }
  18. else if (j == table.Columns.Count - )
  19. {
  20. jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
  21. + "\":" + "\""
  22. + table.Rows[i][j].ToString() + "\"");
  23. }
  24. }
  25. if (i == table.Rows.Count - )
  26. {
  27. jsonString.Append("}");
  28. }
  29. else
  30. {
  31. jsonString.Append("},");
  32. }
  33. }
  34. jsonString.Append("]");
  35. }
  36. return jsonString.ToString();
  37. }

三、调用

  1. DataTable dt = getData();
  2. 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. C和C#的区别

    c:面向过程,语法太麻烦,但对硬件的底层编程和对内存的管理的灵活性方面c是其他高级语言所不可及的. c#:纯面向对象的(跟java很像如果你对java了解估计你就会明白c&c#之间的区别了), ...

  2. 3Des加解密 C#--->Java

    Java的Byte范围为-128至127,c#的Byte范围是0-255.核心是确定Mode和Padding,关于这两个的意思可以搜索3DES算法相关文章. C#采用CBC Mode,PKCS7 Pa ...

  3. angularJS使用rootscope创建父域和子模态框通用的属性与函数

    1. 在声明创建controller指明引用$rootscope reviewInterfaceDo.controller('reviewInterfaceDo', function($scope, ...

  4. WebPack的安装

    一.前提因为webpack是一个基于node的项目,所以首先需要确保你的电脑里面已经安装了node.js,以及npm.在这里我使用的版本是:node:v5.8.0 ,npm:3.7.3,若是版本问题, ...

  5. 深入解读 Js 中的面向对象编程

    前言:今天看了一篇文章觉得很不错,所以给大家分享一下,也许很多人都看过面向对象编程甚至写过这样博客,觉得面向对象编程就那样,没啥好说的,那可能是因为你对这方面知识已经了解,可以选择性跳过.那如果有更通 ...

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

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

  7. 数据库MySQL、redis主从切换shell脚本

    具体源码可找我领取

  8. 【备忘】Idea的那些事

    说到Java的IDE,似乎eclipse和Idea是目前的主流.然而,OO的课程组却一直在推荐使用eclipse,于是很多人就这样错过了Idea这样强大的IDE工具.本文将会对于Idea和Idea的一 ...

  9. hibernate框架学习笔记6:事务

    MySQL的事务.JDBC事务操作: 详细见这篇文章:比较详细 http://www.cnblogs.com/xuyiqing/p/8430214.html 如何在hibernate中配置隔离级别: ...

  10. Rails + React +antd + Redux环境搭建

    前提条件:node和ruby on rails必须已经安装好(相关安装流程不再此处介绍) 1.nvm.node 2.npm or yarn装一个就好 3.rvm.ruby on rails 4.for ...