JSONHelper 的摘要说明

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

/// <summary>
/// JSONHelper 的摘要说明/// </summary>
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 == "leaf" && value=="1")
        {
            value = "true";
            arrData.Add("" + name + ":" + "" + value + "");
        }
        else if (name == "leaf" && value == "0")
        {
            value = "false";
            arrData.Add("" + name + ":" + "" + value + "");
        }
        else if (name == "checked" || name ==  "disabled")    //是否选中
        {
            arrData.Add("" + name + ":" + "" + value + "");
        }
        else
        {
            arrData.Add("" + name + ":\"" + "" + value + "\"");
        }
       
    }



    public void ItemOk()
    {
        arrData.Add("<BR>");
        //返回总记录条数        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 != "<BR>")
                {
                    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;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

/// <summary>
/// JSONHelper 的摘要说明/// </summary>
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 == "leaf" && value=="1")
        {
            value = "true";
            arrData.Add("" + name + ":" + "" + value + "");
        }
        else if (name == "leaf" && value == "0")
        {
            value = "false";
            arrData.Add("" + name + ":" + "" + value + "");
        }
        else if (name == "checked" || name ==  "disabled")    //是否选中
        {
            arrData.Add("" + name + ":" + "" + value + "");
        }
        else
        {
            arrData.Add("" + name + ":\"" + "" + value + "\"");
        }
       
    }



    public void ItemOk()
    {
        arrData.Add("<BR>");
        //返回总记录条数        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 != "<BR>")
                {
                    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();
    }
}

 




//使用案例

 public string GetDTreeSelectInfo(int pareid, string dno)
        {
            DTreeJSONHelper json = new DTreeJSONHelper();
            string jsons = "";
            json.success = true;
            ds = dao.GetDTreeInfo(pareid, dno);
            //如果记录集中对象个数大于0个            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int length = dno.Length;
                    for (int i = 0; i <= length; i++)
                    {
                        if (dr["dno"].ToString() == dno.Substring(0, i))
                        {
                            json.AddItem("id", dr["DID"].ToString());
                            json.AddItem("parentid", dr["PareID"].ToString());
                            json.AddItem("text", dr["DNAME"].ToString());
                            json.AddItem("leaf", dr["Leaf"].ToString());
                            //json.AddItem("checked", "false");
                            json.AddItem("Image", dr["Image"].ToString());
                            json.AddItem("dno", dr["dno"].ToString());
                            json.AddItem("remark", dr["remark"].ToString().Fileter());
                            json.AddItem("Person", dr["Person"].ToString());
                            json.AddItem("Telephone", dr["Telephone"].ToString());
                            json.AddItem("Address", dr["Address"].ToString());
                            json.AddItem("DepCompName", dr["DepCompName"].ToString());
                            json.ItemOk();
                        }
                    }
                    if (dr["dno"].ToString().Length > length)
                    {
                        json.AddItem("id", dr["DID"].ToString());
                        json.AddItem("parentid", dr["PareID"].ToString());
                        json.AddItem("text", dr["DNAME"].ToString());
                        json.AddItem("leaf", dr["Leaf"].ToString());
                        json.AddItem("dno", dr["dno"].ToString());
                        //json.AddItem("checked", "false");
                        json.AddItem("Image", dr["Image"].ToString());
                        json.AddItem("remark", dr["remark"].ToString());
                        json.AddItem("Person", dr["Person"].ToString());
                        json.AddItem("Telephone", dr["Telephone"].ToString());
                        json.AddItem("Address", dr["Address"].ToString());
                        json.AddItem("DepCompName", dr["DepCompName"].ToString());
                        json.ItemOk();
                    }
                }
                jsons = json.ToString();
            }
            else
            {
                jsons = @"{success:false}";
            }

            return jsons;
        }
        public string GetDTreeInfo(int pareid,string dno)
        {
            DTreeJSONHelper json = new DTreeJSONHelper();
            string jsons = "";
            json.success = true;
            ds = dao.GetDTreeInfo(pareid,dno);
            //如果记录集中对象个数大于0个            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int length = dno.Length;
                    for (int i = 0; i <=length;i++)
                    {
                        if (dr["dno"].ToString() == dno.Substring(0, i))
                        {
                            json.AddItem("id", dr["DID"].ToString());
                            json.AddItem("parentid", dr["PareID"].ToString());
                            json.AddItem("text", dr["DNAME"].ToString());
                            json.AddItem("leaf", dr["Leaf"].ToString());
                            //json.AddItem("checked", "false");
                            if (!string.IsNullOrEmpty(dr["Image"].ToString()))
                            {
                                json.AddItem("Image", dr["Image"].ToString());
                            }
                            else
                            {
                                json.AddItem("Image", "kls_01.gif");
                            }
                            //json.AddItem("dno", dr["dno"].ToString());
                            json.AddItem("remark", dr["remark"].ToString().Fileter());
                            json.AddItem("Person", dr["Person"].ToString());
                            json.AddItem("Telephone", dr["Telephone"].ToString());
                            json.AddItem("Address", dr["Address"].ToString());
                            json.AddItem("DepCompName", dr["DepCompName"].ToString());
                            json.AddItem("SMSAccount", dr["SMSAccount"].ToString());
                            json.AddItem("SMSPassword",dr["SMSPassword"].ToString());
                            json.AddItem("EmailHost", dr["EmailHost"].ToString());
                            json.ItemOk();
                        }
                    }
                    if (dr["dno"].ToString().Length > length)
                    {
                        json.AddItem("id", dr["DID"].ToString());
                        json.AddItem("parentid", dr["PareID"].ToString());
                        json.AddItem("text", dr["DNAME"].ToString());
                        json.AddItem("leaf", dr["Leaf"].ToString());
                        //json.AddItem("dno", dr["dno"].ToString());
                        if (!string.IsNullOrEmpty(dr["Image"].ToString()))
                        {
                            json.AddItem("Image", dr["Image"].ToString());
                        }
                        else
                        {
                            json.AddItem("Image", "kls_01.gif");
                        }
                        //json.AddItem("checked", "false");
                        json.AddItem("remark", dr["remark"].ToString());
                        json.AddItem("Person", dr["Person"].ToString());
                        json.AddItem("Telephone", dr["Telephone"].ToString());
                        json.AddItem("Address", dr["Address"].ToString());
                        json.AddItem("DepCompName", dr["DepCompName"].ToString());
                        json.AddItem("SMSAccount", dr["SMSAccount"].ToString());
                        json.AddItem("SMSPassword", dr["SMSPassword"].ToString());
                        json.AddItem("EmailHost", dr["EmailHost"].ToString());
                        json.ItemOk();
                    }
                }
                jsons = json.ToString();
            }
            else
            {
                jsons = @"{success:false}";
            }         

            return jsons;
        }

 

 

 

posted @ 2013-12-21 14:47  AikyZhu  阅读(334)  评论(0编辑  收藏  举报