|
Posted on
2009-04-14 10:22
linFen
阅读( 1015)
评论()
编辑
收藏
举报
- /**
- * 方法名称:DataSetToJson Beat1.0
- * 参数介绍:
- * ds-数据集 ||
- * JsonName-Json数据的根元素名称 ||
- * ParName-需要转化数据集中名称的数组 ||
- * 此方法为测试小样版,因我的项目需要而生,.
- * 初步打算做成类库.可以转化DataTable等.
- * 完成时间:2008-03-14 白色情人节
- **/
- private string DataSetToJson(DataSet ds,string JsonName,string[] ParName)
- {
- try
- {
- if(ds==null)
- {
- return "DataSet Is Null ,So I Can't Do It To Json!";
- }
- if (JsonName.Length < 1)
- {
- return "You Set The Json Name Is Wrong!";
- }
- if (ds.Tables[0].Columns.Count < ParName.Length)
- {
- return "You Give The ParName Is Bigger Than DataSet Columns!";
- }
- string josn = "{ \"" + JsonName + "\":[";
- string temp = "";
- for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
- {
- temp = temp + "{";
- for (int i = 0; i < ParName.Length; i++)
- {
- temp += "\"" + ParName[i] + "\":\"" + ds.Tables[0].Rows[j][ParName[i]] + "\"";
- if (i != ParName.Length - 1)
- {
- temp = temp + ",";
- }
- }
- temp = temp + "},";
- }
- josn = josn + temp + "]}";
- return josn;
- }
- catch (Exception ex)
- {
- return "Codeing is Error----"+ex.ToString();
- }
- }
复制代码
|