如何使用C#c 生成json数据据

C#如何生成json?
C#如何生成json?
楼主你好:在C#中使用Json的话,可以使用.net 3.5平台中所提供的自带方法,介于是C#不是asp.net。所以,在项目中要先添加两个命名空间的引用。1:System.W2 :& System.Web.E做好准备以后,在项目中添加命名空间引用,如下using System.Web.Script.S下面贴代码截图贴运行效果:在这里需要注意的是,.net自带类中支持的JSON序列化源时,需要提供List&T&格式的参数才行。当然也可以代码变动,这里不做介绍。借个位置宣传下: 本人代做asp.net、 C#毕业设计,有需要的请联系
非诚勿扰,谢谢。
那你做过主从明细的b/s功能没?
就是例如下一站订单、主单号下有n条明细记录。
asp.net 不就是B/S么?
说到底就是拼接字符串的过程这里有一篇文章可看看另外附俺写的一个json工具类public static string DataTableToJson(string jsonName, DataTable dt)& & & & {& & & & & & if (CheckTable(dt))& & & & & & {& & & & & & & & StringBuilder json = new StringBuilder();& & & & & & & & bool isHasName = null == jsonName || jsonName.Trim().Equals("");& & & & & & & & if (isHasName)& & & & & & & & & & json.Append("[");& & & & & & & & else& & & & & & & & & & json.Append("{\"").Append(jsonName).Append("\":[");& & & & & & & & foreach (DataRow row in dt.Rows)& & & & & & & & {& & & & & & & & & & json.Append("{");& & & & & & & & & & foreach (DataColumn col in dt.Columns)& & & & & & & & & & {& & & & & & & & & & & & json.Append("\"").Append(col.ColumnName).Append("\":");& & & & & & & & & & & & json.Append("\"");& & & & & & & & & & & & json.Append(row[col].ToString());& & & & & & & & & & & & json.Append("\",");& & & & & & & & & & }& & & & & & & & & & json.Remove(json.Length - 1, 1);& & & & & & & & & & json.Append("},");& & & & & & & & }& & & & & & & & json.Remove(json.Length - 2, 2);& & & & & & & & if (isHasName)& & & & & & & & & & json.Append("}]");& & & & & & & & else& & & & & & & & & & json.Append("}]}");& & & & & & & & return json.ToString();& & & & & & }& & & & & & return "[]";& & & & }
略懂社热议
json只是一种数据格式,其实就是一段按照格式来的字符串
System.Web.Script.Json 下的可序列化对象
这个是验证json 格式的网站你给我个邮箱 我把json 的序列化 和反序列化 .class 发给你
json只是一种数据格式,得看你要怎么弄,最简单的就是通过ajax来导出
略懂社热议
等待您来回答
编程领域专家
&SOGOU - 京ICP证050897号c#处理3种json数据的实例
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了c#处理包含数组、对象的复杂json数据的方法,,需要的朋友可以参考下
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋友。
一、C#处理简单json数据
json数据:
代码如下:{"result":"0","res_info":"ok","queryorder_info":"info"}
我这里是以jsonmy1.txt文件的形式保存在d盘json文件夹下。
构造对象:
&public struct ToJsonMy
&&& public string result { }& //属性的名字,必须与json格式字符串中的"key"值一样。
&&& public string res_info { }
&&& public string queryorder_info { }&&
转换过程:
代码如下: public static void JsonMy()
&&& string json = Jsonstr("D:\\json\\jsonmy1.txt");//Jsonstr函数读取json数据的文本txt&&&&&&&&&       
&&& JavaScriptSerializer js = new JavaScriptSerializer();&& //实例化一个能够序列化数据的类
&&& ToJsonMy list = js.Deserialize&ToJsonMy&(json);&&& //将json数据转化为对象类型并赋值给list
&&& string result = list.&&&&&        
&&& string res_info = list.res_
&&& string queryorder_info = list.res_
二、C#处理包含对象的json数据
json数据:jsonmy2.txt
代码如下:{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}
构造对象:
public struct ToJsonMy2
&&& public string result { }&
&&& public string res_info { }
&&& public queryorder_info queryorder_
public struct queryorder_info
&&& public string order_num { }
&&& public string orderdetail { }
转换过程:
代码如下:public static void JsonMy2()
&&& string json = Jsonstr("D:\\json\\jsonmy2.txt");
&&& JavaScriptSerializer js = new JavaScriptSerializer();&& //实例化一个能够序列化数据的类
&&& ToJsonMy2 list = js.Deserialize&ToJsonMy2&(json);&&& //将json数据转化为对象类型并赋值给list
&&& string result = list.&&&
&&& string res_info = list.res_
&&& string order_num = list.queryorder_info.order_
&&& string orderdetail = list.queryorder_info.
三、C#处理包含对象、数组的json数据
json数据:jsonmy4.txt
代码如下:{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":[{"CFTUin":"","CancelDeadline":" 23:00:00","CheckInDate":" 00:00:00","CheckOutDate":" 00:00:00","CityID":"0101","CurrencyCode":"RMB","HotelID":"","HotelName":"乐家连锁(北京天坛南门店)(原速8酒店(北京天坛南门店)","ListID":"9652","PayAmt":"228","PayType":"0","RommsCnt":"1","SPTransID":"","State":"4"},{"CFTUin":"","CancelDeadline":" 23:00:00","CheckInDate":" 00:00:00","CheckOutDate":" 00:00:00","CityID":"0201","CurrencyCode":"RMB","HotelID":"","HotelName":"上海凯顿酒店","ListID":"9413","PayAmt":"1140","PayType":"0","RommsCnt":"1","SPTransID":"","State":"4"}]}}
构造对象:
代码如下:public struct ToJsonMy3
&&& public string result { }
&&& public string res_info { }
&&& public queryorder_info queryorder_
public struct queryorder_info
&&& public string order_num { }
&&& public List&orderdetail&//数组处理&&&&&&
public struct orderdetail
&&& public string CFTUin { }
&&& public string CancelDeadline { }
&&& public string CheckInDate { }
&&& public string CheckOutDate { }
&&& public string CityID { }
&&& public string CurrencyCode { }
&&& public string HotelID { }
&&& public string HotelName { }
&&& public string ListID { }
&&& public string PayAmt { }
&&& public string PayType { }
&&& public string RommsCnt { }
&&& public string SPTransID { }
&&& public string State { }
转换过程:
代码如下:public static void JsonMy4()
&&& string json = Jsonstr("D:\\json\\jsonmy4.txt");
&&& JavaScriptSerializer js = new JavaScriptSerializer();&& //实例化一个能够序列化数据的类
&&& ToJsonMy3 list = js.Deserialize&ToJsonMy3&(json);&&& //将json数据转化为对象类型并赋值给list
&&& string result = list.&&&&
&&& string res_info = list.res_
&&& string order_num = list.queryorder_info.order_
&&& List&orderdetail& orderdetail = list.queryorder_info.
&&& string CFTUin = orderdetail[0].CFTU
&&& string HotelName = orderdetail[0].HotelN
&&& string ListID = orderdetail[1].ListID;
&&& string State = orderdetail[2].S
&PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
JSON在线格式化工具:
在线XML/JSON互相转换工具:
json代码在线格式化/美化/压缩/编辑/转换工具:
在线json压缩/转义工具:
C语言风格/HTML/CSS/json代码格式化美化工具:
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具C# 如何生成树状的Json数据
[问题点数:100分,结帖人fengyunzb1]
C# 如何生成树状的Json数据
[问题点数:100分,结帖人fengyunzb1]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。怎么使用C#生成json数据_怎么处理阿!_截取字符及失去数量__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
怎么使用C#生成json数据
怎么处理阿!
截取字符及失去数量
怎么使用C#生成json数据
如何使用C#生成json数据[ ["崇明", "崇明"], ["黄浦", "黄浦"], ["卢湾", "卢湾"], ["徐汇", "徐汇"], ["长宁", "长宁"], ["静安", "静安"], ["普陀", "普陀"], ["闸北", "闸北"], ["虹口", "虹口"], ["杨浦", "杨浦"], ["闵行", "闵行"], ["宝山", "宝山"], ["嘉定", "嘉定"], ["浦东", "浦东"], ["金山", "金山"], ["松江", "松江"], ["青浦", "青浦"], ["南汇", "南汇"], ["奉贤", "奉贤"], ["朱家角", "朱家角"]]如何使用C#生成上面的json数据------解决方案--------------------分两次贴
#region Datatable转换为Json
/// &summary&
/// Datatable转换为Json
/// &/summary&
/// &param name="table"&Datatable对象&/param&
/// &returns&Json字符串&/returns&
public static string ToJson(DataTable dt)
StringBuilder jsonString = new StringBuilder();
jsonString.Append("[");
DataRowCollection drc = dt.R
for (int i = 0; i & drc.C i++)
jsonString.Append("{");
for (int j = 0; j & dt.Columns.C j++)
string strKey = dt.Columns[j].ColumnN
string strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataT
jsonString.Append("\"" + strKey + "\":");
strValue = StringFormat(strValue, type);
if (j & dt.Columns.Count - 1)
jsonString.Append(strValue + ",");
jsonString.Append(strValue);
jsonString.Append("},");
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
return jsonString.ToString();
/// &summary&
/// DataTable转换为Json
/// &/summary&
public static string ToJson(DataTable dt, string jsonName)
StringBuilder Json = new StringBuilder();
if (string.IsNullOrEmpty(jsonName)) jsonName = dt.TableN
Json.Append("{\"" + jsonName + "\":[");
if (dt.Rows.Count & 0)
for (int i = 0; i & dt.Rows.C i++)
Json.Append("{");
for (int j = 0; j & dt.Columns.C j++)
Type type = dt.Rows[i][j].GetType();
Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + StringFormat(dt.Rows[i][j].ToString(), type));
if (j & dt.Columns.Count - 1)
Json.Append(",");
Json.Append("}");
if (i & dt.Rows.Count - 1)
Json.Append(",");
Json.Append("]}");
return Json.ToString();
#endregion
#region DataReader转换为Json
/// &summary&
/// DataReader转换为Json
/// &/summary&
/// &param name="dataReader"&DataReader对象&/param&
/// &returns&Json字符串&/returns&
public static string ToJson(DbDataReader dataReader)
StringBuilder jsonString = new StringBuilder();
jsonString.Append("[");
while (dataReader.Read())
jsonString.Append("{");
for (int i = 0; i & dataReader.FieldC i++)
Type type = dataReader.GetFieldType(i);
string strKey = dataReader.GetName(i);
string strValue = dataReader[i].ToString();
jsonString.Append("\"" + strKey + "\":");
strValue = StringFormat(strValue, type);
if (i & dataReader.FieldCount - 1)
jsonString.Append(strValue + ",");
jsonString.Append(strValue);
jsonString.Append("},");
dataReader.Close();
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
return jsonString.ToString();
#endregion
------解决方案--------------------RegionInfo须加上Serializable
[Serializable]
public class RegionInfo
怎么处理阿!
怎么办阿!!!编译报了这些错,不知道怎么办了?高手指点下MySimulation.cppc:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const class std::deque&_Ty,_A& &,const class std::deque&_Ty,_A& &)' : could not deduce template argument for 'const class std::deque&_Ty,_A& &' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const class std::vector&_Ty,_A& &,const class std::vector&_Ty,_A& &)' : could not deduce template argument for 'const class std::vector&_Ty,_A& &' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const class std::basic_string&_E,_Tr,_A& &,const _E *)' : could not deduce template argument for 'const class std::basic_string&_E,_Tr,_A& &' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const _E *,const class std::basic_string&_E,_Tr,_A& &)' : could not deduce template argument for 'const
*' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const class std::basic_string&_E,_Tr,_A& &,const class std::basic_string&_E,_Tr,_A& &)' : could not deduce template argument for 'const
class std::basic_string&_E,_Tr,_A& &' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const class std::reverse_iterator&_RI,_Ty,_Rt,_Pt,_D& &,const class std::reverse_iterator&_RI,_Ty,_Rt,_Pt,_D& &)' : could not deduce template argument for 'const class std::reverse_iterator&_RI,_Ty,_Rt,_Pt,_D& &' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator &(const struct std::pair&_T1,_T2& &,const struct std::pair&_T1,_T2& &)' : could not deduce template argument for 'const struct std::pair&_T1,_T2& &' from 'const class Event'
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'
c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2676: binary '&' : 'const class Event' does not define this operator or a conversion to a type acceptable to the predefined operator
c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less&class Event&::operator ()(const class Event &,const class Event &) const'Error executing cl.exe.MySimulation.obj - 8 error(s), 0 warning(s)------解决方案--------------------没实现bool operator& (const Event&, const Event&);?瞎猜。。
------解决方案--------------------C/C++ code
是不是Simulation头文件中没有加#include"Event.h"
这里使用了啊,
priority_queue
还有就是Event类的定义问题了。
截取字符及失去数量
截取字符及得到数量123大哥/456小二/789礼物/...
如上,怎么分别取得
st1:123大哥
st2:456小二
st3:789礼物
结果值是:3列
判断字符"/"
------解决方案--------------------int count = 0
string ls_source = "123大哥/456小二/789礼物/...", ls_buf
string lsa_result[]
ls_buf = f_get_token(ls_target)
if ls_buf = "" then exit
lsa_result[count] = ls_buf
loop while true
// f_get_token()
功能:(重载)
按指定的分隔符分解另一个字符串(返回分隔符前的字符串,并把它从源字符串中删除)
源字串(输出)
separator: 分隔符
返回字符串
String Function GET_TOKEN (ref string Source, readonly string Separator)
The function Get_Token receive, as arguments, the string from which
the token is to be stripped off, from the left, and the separator
character.
If the separator character does not appear in the string,
it returns the entire string.
Otherwise, it returns the token, not
including the separator character.
In either case, the source string
is truncated on the left, by the length of the token and separator
character, if any.
p = Pos(source, separator) // Get the position of the separator
if p = 0 then
// if no separator,
ret = source
// return the whole source string and
source = ""
// make the original source of zero length
ret = Mid(source, 1, p - 1) // otherwise, return just the token and
source = Right(source, Len(source) - p) // strip it & the separator
return ret
------解决方案--------------------不好意思,漏了分隔符;而且不够严谨!
// 严谨的写法
ls_buf = f_get_token(ls_source, "/")
lsa_result[count] = ls_buf
messagebox("第 " + string(count) + " 列", lsa_result[count])
loop while ls_source && ""
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:}

我要回帖

更多关于 随机json数据生成器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信