C# 处理Json

 下面是JSON对象转换为字符串

 public static string ToJson(object obj)
        {
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var output = serializer.Serialize(obj);
                return output;
            }
            catch (Exception ex)
            {
                
                return "";
            }
        }
 public static T FromJson<T>(string jsonStr)
        {
            try
            {
                JavaScriptSerializer ds = new JavaScriptSerializer();
                return (T)ds.Deserialize(jsonStr, typeof(T));
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }

 上面是JSON字符串转换为JSON对象;

备注 如果是在visual studio 中可以复制JSON字符串-》vs编辑-》选择性粘贴-》将JSON粘贴为类;

 

posted on 2015-03-26 18:34  夜雨梧桐  阅读(304)  评论(0编辑  收藏  举报

导航