C# JsonHelper

/// <summary>
///Json 的摘要说明
/// </summary>
public class Json
{
    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 Json()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    //对应于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;
        arrData.Clear();
        arrDataItem.Clear();
    }



    public void AddItem(string name, string value)
    {
        arrDataItem.Add(name);
        arrDataItem.Add(value);
    }



    public void ItemOk()
    {
        arrData.Add(arrDataItem);
        arrDataItem = new ArrayList();
        //返回总记录条数
        totlalCount++;
    }

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

        int ad = arrData.Count;
        for (int i = 0; i < ad; i++)
        {
            ArrayList arr = (ArrayList)(arrData[i]);
            sb.Append("{");
            int t = arr.Count;
            for (int j = 0; j < t; j += 2)
            {
                if (j == t)
                    break;
                sb.Append("\"");
                sb.Append(arr[j].ToString());
                sb.Append("\"");
                sb.Append(":");
                sb.Append("\"");
                sb.Append(arr[j + 1].ToString());
                sb.Append("\"");
                if (j < t - 2)
                    sb.Append(",");
            }
            sb.Append("}");
            if (i < ad - 1)
                sb.Append(",");
        }

        sb.Append("]");
        sb.Append("}");
        return sb.ToString();
    }
}


前台取数据:

$.post("Default.aspx", { flag: "ViewPage" }, function (dataStr) {
                var dataObj = eval("(" + dataStr + ")");
                if (dataObj.error != "") {
                    $("#myGrid").hide();
                    $("#errorTip").show();
                    $("#errorTip").html(dataObj.error);
                } else {
                    if (dataObj.data.length > 0) {
                        for (var i = 0; i < dataObj.data.length; i++) {
                            var d = (data[i] = {});
                            d["PageID"] = dataObj.data[i]["PageID"];
                            d["PageName"] = dataObj.data[i]["PageName"];
                            d["PageSeq"] = dataObj.data[i]["PageSeq"];
                            d["DisplayInterval"] = dataObj.data[i]["DisplayInterval"];
                            d["Display"] = dataObj.data[i]["Display"];
                        }
                        grid = new Slick.Grid("#myGrid", data, columns, options);
                    }
                }
            });


你可能感兴趣的:(C# JsonHelper)