zoukankan      html  css  js  c++  java
  • JSONHelper.CS三层架构中搜集整理的代码

    参考网址:http://www.cnblogs.com/shenshi/archive/2009/05/19/1460466.html

    1。添加系统引用

    //添加引用VS2005中添加System.Web.Extensions.dll的引用
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Data;
    using System.Collections;

    2.详细代码   

        /// <summary>
        /// JSONHelper 的摘要说明
        /// </summary>
        public class JSONHelper
        {
            //对应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 JSONHelper()
            {

            }

            //对应于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)
            {
                arrData.Add("/"" + name + "/":" + "/"" + value + "/"");
            }

            public void ItemOk()
            {
                arrData.Add("<BR>");
                totlalCount++;
            }

            //序列化JSON对象,得到返回的JSON代码
            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                sb.Append("totalCount:" + totlalCount.ToString() + ",");
                sb.Append("success:" + _success.ToString().ToLower() + ",");
                sb.Append("error:/"" + _error.Replace("/"", "///"") + "/",");
                sb.Append("singleInfo:/"" + singleInfo.Replace("/"", "///"") + "/",");
                sb.Append("data:[");

                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();
            }
        }

  • 相关阅读:
    9.经典进程同步问题
    8.信号量
    7.进程同步之临界区问题
    6.CPU调度
    AIX 10G HA RAC卸载
    HP11.31安装11.2.0.3实施手册
    AIX上安装oracle10g
    习水医院12C RAC 数据库安装文档
    linux 11201(11203) ASM RAC 安装
    如果一个节点重新安装了,处理办法
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306795.html
Copyright © 2011-2022 走看看