[心得] JSON扫盲帖+JSON类教程

http://bbs.9ria.com/thread-1657-1-1.html

Parse JSON in C#

http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp

我的JsonHelper:

public static class JSONHelper
    {
        public static T Deserialise<T>(this string setting) where T : class
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(setting)))
            {
                return serializer.ReadObject(ms) as T;
            }
        }

        public static string Serialise(this object obj)
        {
            return Serialise(obj, obj.GetType());
        }

        public static string Serialise(this object obj, Type type)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(type);

            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

 

posted on 2012-12-06 10:21  higirle  阅读(542)  评论(0编辑  收藏  举报