博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 后台构造json数据
阅读量:5257 次
发布时间:2019-06-14

本文共 3181 字,大约阅读时间需要 10 分钟。

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

构造的json串格式,如下:

[{
"id":"0","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"1","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"2","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"3","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"4","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"5","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"6","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"7","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"8","name":"name","active":"active","user_id":"user_id","no_of_reports":"no_of_reports"}, {
"id":"9","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 = 0; i < 10; 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 > 0)            {                jsonString.Append("[");                for (int i = 0; i < table.Rows.Count; i++)                {                    jsonString.Append("{
"); for (int j = 0; j < table.Columns.Count; j++) { if (j < table.Columns.Count - 1) { jsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\","); } else if (j == table.Columns.Count - 1) { jsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\""); } } if (i == table.Rows.Count - 1) { jsonString.Append("}"); } else { jsonString.Append("},"); } } jsonString.Append("]"); } return jsonString.ToString(); }

三、调用

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

 

转载于:https://www.cnblogs.com/zhangjd/p/7884858.html

你可能感兴趣的文章
Altera Quartus II 12.0订购版下载
查看>>
移动端踩坑系列(一)————input默认调用数字键盘
查看>>
第十八次ScrumMeeting会议
查看>>
mysql部分替换
查看>>
IOS关于数据加密(主要为登录加密)想总结的
查看>>
微信公众号导出关注的用户信息
查看>>
test
查看>>
使用ProGuard使你android代码保持混淆
查看>>
android listVIew实现button按钮监听程序
查看>>
[C++] getaddrinfo的一个bug
查看>>
perl C/C++ 扩展(四)
查看>>
BZOJ2763: [JLOI2011]飞行路线
查看>>
【c# 学习笔记】阻止派生类重写虚成员
查看>>
Django 路由
查看>>
fcgi返回状态码
查看>>
pycharm 中查找替换功能
查看>>
【特征匹配】BRISK原文翻译
查看>>
grep 基于关键字搜索
查看>>
virtualbox开启虚拟机时报错
查看>>
HDU5461 Largest Point(暴力)
查看>>