C#生成JSON数据格式的函数

使用说明:
    对List<类> 集合使用的方法:
     List<TBL_CODE_MZ> objList = objAction.GetCODE_MByList(objTbl);
     string JsonSource = JSONHelper.ToJSON(objList); //当前页记录转成JSON格式
     但有个问题:
     日期型数据谁转换成:@123456789@这样的数据!???不知道谁碰到过没有!!!!

  对数据集的使用如:DataSet,DataTable
 


JSONHelper jsonHelp = new JSONHelper();

            jsonHelp.success 
= true;
            jsonHelp.totlalCount 
= getPrjListCount();
            
            
foreach (DataRow dr in dsResult.Tables[0].Rows)
            {
                jsonHelp.AddItem(
"PRJ_ID", dr["PRJ_ID"].ToString());
                jsonHelp.AddItem(
"PRJ_NAME", dr["PRJ_NAME"].ToString());
                jsonHelp.AddItem(
"DEP_ID", dr["DEP_ID"].ToString());
                jsonHelp.AddItem(
"PRJ_MANID", dr["PRJ_MANID"].ToString());
                jsonHelp.AddItem(
"PRJ_STATE", dr["PRJ_STATE"].ToString());

                
if (dr["PRJ_STARTDATE"!= DBNull.Value)
                {
                    jsonHelp.AddItem(
"PRJ_STARTDATE", Convert.ToDateTime(dr["PRJ_STARTDATE"]).ToString("yyyy/MM/dd"));
                }
                
else
                {
                    jsonHelp.AddItem(
"PRJ_STARTDATE"string.Empty);
                }
                
if (dr["PRJ_ENDDATE"!= DBNull.Value)
                {
                    jsonHelp.AddItem(
"PRJ_ENDDATE", Convert.ToDateTime(dr["PRJ_ENDDATE"]).ToString("yyyy/MM/dd"));
                }
                
else
                {
                    jsonHelp.AddItem(
"PRJ_ENDDATE"string.Empty);
                }
                                
                jsonHelp.AddItem(
"PRJ_MEMO", dr["PRJ_MEMO"].ToString());
                jsonHelp.AddItem(
"CODE_NAME", dr["CODE_NAME"].ToString());
                jsonHelp.AddItem(
"DEP_NAME", dr["DEP_NAME"].ToString());
                jsonHelp.AddItem(
"USER_NAME", dr["USER_NAME"].ToString());
                jsonHelp.ItemOk();
            }

            
string strResult = jsonHelp.ToString();

 

//######################JSONHelp.cs源码######################################//


using System;
using System.Collections;
using System.Collections.Generic; 
using System.Text; 
using System.Web.Script.Serialization;

namespace framework.common
{
    
/**//// <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();
        
               
        
public JSONHelper()
        {
            
        }
        
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成员
        设置success为TURE,代表成功#region 设置success为TURE,代表成功
        
public bool success
        {
            
get
            {
                
return _success;
            }
            
set
            {
                
//如设置为true则清空error
                if (success) _error = string.Empty;
                _success 
= value;
            }
        }
        
#endregion
        
//对应于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();
        }
        
#region

        
public void AddItem(string name, string value)
        {
            arrData.Add(
""" + name + "":" + """ + value + """);
        }
        
        
#endregion


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

        
//序列化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 - 11);
                        sb.Append(
"},");
                        
if (index < arrData.Count)
                        {
                            sb.Append(
"{");
                        }
                    }
                   
                }
                sb 
= sb.Replace(",""", sb.Length - 11);
                sb.Append(
"]");
            }
            
            sb.Append(
"}");
            
return sb.ToString();
        }
    }
}

你可能感兴趣的:(json)