using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
//using System.Web.Script.Serialization;
using System.Runtime.Serialization;
///
/// JSONHelper 的摘要说明
///
public class DTreeJSONHelper
{
//对应JSON的singleInfo成员
public string singleInfo = string.Empty;
protected string _error = string.Empty;
protected bool _success = true;
protected long _totalCount = 0;
protected System.Collections.ArrayList arrData = new ArrayList();
protected System.Collections.ArrayList arrDataItem = new ArrayList();
public DTreeJSONHelper()
{
}
//public static string ToJSON(object obj)
//{
// JavaScriptSerializer serializer = new JavaScriptSerializer();
// return serializer.Serialize(obj);
//}
//public static string ToJSON(object obj, int recursionDepth)
//{
// JavaScriptSerializer serializer = new JavaScriptSerializer();
// serializer.RecursionLimit = recursionDepth;
// return serializer.Serialize(obj);
//}
//对应于JSON的success成员
public bool success
{
get
{
return _success;
}
set
{
//如设置为true则清空error
if (success) _error = string.Empty;
_success = value;
}
}
//对应于JSON的error成员
public string error
{
get
{
return _error;
}
set
{
//如设置error,则自动设置success为false
if (value != "") _success = false;
_error = value;
}
}
public long totlalCount
{
get { return _totalCount; }
set { _totalCount = value; }
}
//重置,每次新生成一个json对象时必须执行该方法
public void Reset()
{
_success = true;
_error = string.Empty;
singleInfo = string.Empty;
arrData.Clear();
arrDataItem.Clear();
}
public void AddItem(string name, string value)
{
if (name == "checked" && value == "1")
{
value = "true";
arrData.Add("'" + name + "':" + "" + value + "");
}
else if (name == "checked" && value == "0")
{
value = "false";
arrData.Add("'" + name + "':" + "" + value + "");
}
else if (name == "leaf" && value=="1")
{
value = "true";
arrData.Add("'" + name + "':" + "" + value + "");
}
else if (name == "leaf" && value == "0")
{
value = "false";
arrData.Add("'" + name + "':" + "" + value + "");
}
else
{
arrData.Add("'" + name + "':\"" + "" + value + "\"");
}
}
public void ItemOk()
{
arrData.Add("
");
//返回总记录条数
totlalCount++;
}
//序列化JSON对象,得到返回的JSON代码
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
int index = 0;
sb.Append("{");
if (arrData.Count <= 0)
{
sb.Append("}");
}
else
{
foreach (string val in arrData)
{
index++;
if (val != "
")
{
sb.Append(val + ",");
}
else
{
sb = sb.Replace(",", "", sb.Length - 1, 1);
sb.Append("},");
if (index < arrData.Count)
{
sb.Append("{");
}
}
}
sb = sb.Replace(",", "", sb.Length - 1, 1);
sb.Append("");
}
sb.Append("]");
return sb.ToString();
}
}
//=============================================调用例子=============================
using System;
using System.Collections.Generic;
using System.Text;
//using Model;
using DAL;
using System.Data;
namespace BLL
{
public class MenuTree
{
private DataSet ds;
DAL.MenuTree dao = new DAL.MenuTree();
//查询权限树信息
public string GetDtreeInfos(string parentID)
{
string DTreeJSON="";
DTreeJSONHelper json = new DTreeJSONHelper();
try
{
ds=dao.getDTreeInfo(parentID);
json.success = true;
foreach(DataRow dr in ds.Tables[0].Rows)
{
json.AddItem("href", dr["href"].ToString());
json.AddItem("hrefTarget", "MainFram");
json.AddItem("id", dr["id"].ToString());
//json.AddItem("parentid", dr["parentid"].ToString());
json.AddItem("text", dr["title"].ToString());
json.AddItem("leaf", dr["leaf"].ToString());
//json.AddItem("icon", dr["icon"].ToString());
//json.AddItem("number", dr["number"].ToString());
json.AddItem("qtip", dr["title"].ToString());
//json.AddItem("checked", "1");
json.ItemOk();
}
DTreeJSON = json.ToString();
}
catch (Exception)
{
throw;
}
return DTreeJSON;
}
}
}