博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSONHelper 的摘要说明
阅读量:6614 次
发布时间:2019-06-24

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

using System;using System.Collections;using System.Collections.Generic;using System.Text;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 == "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("
"); //返回总记录条数 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;using System.Collections.Generic;using System.Text;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 == "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("
"); //返回总记录条数 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(); }}
 

 

 
//使用案例 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;        }
 

 

 

 

转载于:https://www.cnblogs.com/aiky/p/3485130.html

你可能感兴趣的文章
[裴礼文数学分析中的典型问题与方法习题参考解答]5.1.24
查看>>
深度学习之江湖~那些大神们
查看>>
Configure Apache Virtual Hosts - CentOS 7
查看>>
8.5. profile
查看>>
C# 动态解析表达式
查看>>
C语言 编程练习22题
查看>>
Android TextView中显示图片
查看>>
使用keepalived和HaVip搭建具备高可用能力的SNAT网关
查看>>
Net高并发解决思路
查看>>
Log4Net 生成多个文件、文件名累加解决方法
查看>>
ARMS 公有云 发布 V2.3.1版本, 新增 应用监控功能 等重磅功能。
查看>>
Oracle中REGEXP_SUBSTR函数
查看>>
Xamarin.Android开发实践(十三)
查看>>
如果你建造了一个精良的模型却没人用,你肯定不会得到赞誉(转)
查看>>
Bootstrap<基础三> 排版
查看>>
【Cocos2d-x】开发基础-Cocos简介与环境搭建
查看>>
SQL查询性能分析
查看>>
Json——js和C#对Json的操作
查看>>
oracle 包,函数,过程,块的创建和执行及在java中执行(转)
查看>>
CloudDBA现场助力双十一
查看>>