zoukankan      html  css  js  c++  java
  • .NET中JSON的序列化和反序列化

    .NET 中有两种方法进行JSON的操作分别需要引用不同的命名空间

    1.System.Runtime.Serialization.Json(System.Runtime.Serialization.dll)

    public class JsonHelper
    {
        /// <summary>
        /// 生成Json格式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetJson<T>(T obj)
        {
            DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, obj);
                string szJson = Encoding.UTF8.GetString(stream.ToArray()); return szJson;
            }
        }
        /// <summary>
        /// 获取Json的Model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="szJson"></param>
        /// <returns></returns>
        public static T ParseFromJson<T>(string szJson)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }
    }
    
    
    public class topMenu
    {
        public string id { get; set; }
        public string title { get; set; }
        public string defaulturl { get; set; }
    }

    具体的操作代码

    topMenu t_menu = new topMenu()
                {
                    id = "1",
                    title = "全局",
                    defaulturl = "123456"
                };
    List<topMenu> l_topmenu = new List<topMenu>();
    for (int i = 0; i < 3; i++)
                {
                    l_topmenu.Add(t_menu);
                }
    Response.Write(JsonHelper.GetJson<List<topMenu>>(l_topmenu));

    输出结果为:
           [{"defaulturl":"123456","id":"1","title":"全局"},{"defaulturl":"123456","id":"1","title":"全局"},{"defaulturl":"123456","id":"1","title":"全局"}] 

    下面利用上面ParseFromJson方法读取Json
         输出结果为:全局

    string szJson = @"{""id"":""1"",""title"":""全局"",""defaulturl"":""123456""} ";
          topMenu t_menu2 = JsonHelper.ParseFromJson<topMenu>(szJson);
          Response.Write(t_menu2.title);

    2.System.Web.Script.Serialization(System.Web.Extensions.dll)

    JavaScriptSerializer jss = new JavaScriptSerializer(); 
    Response.Write( jss.Serialize(l_topmenu )); 
    string szJson = @"{""id"":""1"",""title"":""全局"",""defaulturl"":""123456""} ";
    topMenu toptabmenu = jss.Deserialize<topMenu>(szJson); 
    Response.Write( jss.Serialize(toptabmenu.title)); 
  • 相关阅读:
    Prim算法以及Kruskal算法
    PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
    PAT甲级考前整理(2019年3月备考)之一
    PAT甲级考前整理(2019年3月备考)之二,持续更新中.....
    linux下挂载U盘
    opencv2已有的情况下,安装opencv3以及对应的opencv_contrib
    二维数组和二级指针做函数参数的问题
    ubuntu14.04下 python2.7怎么链接到安装在指定文件夹的opencv3
    使用opencv的nonfree模块
    PaddlePaddle开源平台的应用
  • 原文地址:https://www.cnblogs.com/answercard/p/4916087.html
Copyright © 2011-2022 走看看