JSON Helper

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.ServiceModel.Web;///记得引用这个命名空间

using System.Runtime.Serialization.Json;

using System.IO;

using System.Text;



namespace Sigil.Common

{

    /// <summary>

    /// JSON操作类

    /// </summary>

    public class JsonHelper

    {

        /// <summary>

        /// 把对象序列化 JSON 字符串 

        /// </summary>

        /// <typeparam name="T">对象类型</typeparam>

        /// <param name="obj">对象实体</param>

        /// <returns>JSON字符串</returns>

        public static string GetJson<T>(T obj)

        {

            //记住 添加引用 System.ServiceModel.Web 

            /**

             * 如果不添加上面的引用,System.Runtime.Serialization.Json; Json是出不来的哦

             * */

            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));

            using (MemoryStream ms = new MemoryStream())

            {

                json.WriteObject(ms, obj);

                string szJson = Encoding.UTF8.GetString(ms.ToArray());

                return szJson;

            }

        }



        /// <summary>

        /// Json反序列化,用于接收客户端Json后生成对应的对象

        /// </summary>

        /// <typeparam name="T">对象类型</typeparam>

        /// <param name="jsonString">JSON字符串</param>

        /// <returns>对象实体</returns>

        public static T ParseFormJson<T>(string jsonString)

        {

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))

            {

                return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);

            }

        }



        /// <summary>

        /// JSON序列化

        /// </summary>

        public static string JsonSerializer<T>(T t)

        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            MemoryStream ms = new MemoryStream();

            ser.WriteObject(ms, t);

            string jsonString = Encoding.UTF8.GetString(ms.ToArray());

            ms.Close();

            return jsonString;

        }



        /// <summary>

        /// JSON反序列化

        /// </summary>

        public static T JsonDeserialize<T>(string jsonString)

        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));

            T obj = (T)ser.ReadObject(ms);

            return obj;

        }

    }

}

参考: 

  http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html

      http://www.cnblogs.com/yylp521/p/3148856.html

你可能感兴趣的:(json)