ExtJs JsonHelper Json 서열화 및 반서열화

3979 단어 jsonWebLINQ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.Reflection;

namespace CommonHelper
...{
    public static class JSONHelper
    ...{
        /**//// <summary>
        ///       Json   
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJSON(this object obj)
        ...{            
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var st = serializer.Serialize(obj).Replace(""", "'");
            return st;
        }

        /**//// <summary>
        ///        
        /// </summary>
        /// <param name="obj">   </param>
        /// <param name="selfName">       </param>
        /// <param name="others">      </param>
        /// <returns></returns>
        public static string ToJSON(this object obj, string selfName, object others)
        ...{
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.MaxJsonLength = int.MaxValue;
            SortedList<string, object> data = new SortedList<string, object>();
            data.Add(selfName, obj);
            PropertyInfo[] pis = others.GetType().GetProperties();
            foreach (PropertyInfo pi in pis)
            ...{
                data.Add(pi.Name,pi.GetValue(others,null));
            }
            return serializer.Serialize(data).Replace(""", "'");
        }

        /**//// <summary>
        ///      
        /// </summary>
        /// <param name="obj">   </param>
        /// <param name="selfName">     </param>
        /// <returns></returns>
        public static string ToJSON(this object obj, string selfName)
        ...{
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            SortedList<string, object> data = new SortedList<string, object>();
            data.Add(selfName, obj);
            string s = data.ToJSON();
            return s.Replace(""", "'");
        }

        /**//// <summary>
        ///      
        /// </summary>
        /// <param name="obj">   </param>
        /// <param name="recursionDepth">                       ,     100;         1       System.ArgumentOutOfRangeException</param>
        /// <returns></returns>
        public static string ToJSON(this object obj, int recursionDepth)
        ...{
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            serializer.RecursionLimit = recursionDepth;
            return serializer.Serialize(obj).Replace(""", "'");
        }

        /**//// <summary>
        ///   JSON          
        /// </summary>
        /// <typeparam name="T">      </typeparam>
        /// <param name="json">Json   </param>
        /// <returns></returns>
        public static T DeJSON<T>(this string json)
        ...{
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<T>(json);
        }
    }
}

좋은 웹페이지 즐겨찾기