Fork me on GitHub

李可

导航

序列化各个类型成JSON XML实例

LitJson组件序列化请参考http://www.cnblogs.com/leee/p/4437230.html

XML序列化请参考http://www.cnblogs.com/leee/p/4244549.html

这篇实例是关于JavaScriptSerializer对各个类型的序列化。

将一个类的实例序列成json

    public class Person
  {
  
     public string Name { get; set; }
     public int Age { get; set; }
  }

   Person p1 = new Person();
   p1.Name = "like";
   p1.Age = 24;
   JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
   var json = javascriptSerializer.Serialize(p1);
   Response.Write(json);//{"Name":"like","Age":24}

  

将一个序列序列成“数组”

            string[] arr = new string[] { "s", "dd", "fff" };
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            var json = javascriptSerializer.Serialize(arr);
            Response.Write(json);//["s","dd","fff"]

  

将一个list序列成“数组”

            List<string> list = new List<string>();
            list.Add("sss");
            list.Add("dd");
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            var json = javascriptSerializer.Serialize(list);
            Response.Write(json);//["sss","dd"]

  

将一个无序字典hashtable序列成json

            Hashtable has = new Hashtable();
            has.Add("1", "sada");
            has.Add("5", "sadjdi");
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            var json = javascriptSerializer.Serialize(has);
            Response.Write(json);//{"1":"sada","5":"sadjdi"}

  

将一个泛型字典序列成json

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("first", "sasa");
            dic.Add("second", "dddd");
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            var json = javascriptSerializer.Serialize(dic);
            Response.Write(json);//{"first":"sasa","second":"dddd"}

  

将一个Datatable序列成数组

public string DateTableToJson(DataTable dt)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    result.Add(dc.ColumnName, dr[dc]);
                }
                list.Add(result);
            }
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
            var json = javascriptSerializer.Serialize(list);
            return json;
        }

手动写一个datatable 

            //手动写一个datatable
            DataTable dt = new DataTable("cart");
            DataColumn l0 = new DataColumn("编号", Type.GetType("System.Int32"));
            DataColumn l1 = new DataColumn("姓名", Type.GetType("System.String"));
            DataColumn l2 = new DataColumn("年龄", Type.GetType("System.Int16"));
            DataColumn l3 = new DataColumn("身高", Type.GetType("System.Int16"));
            DataColumn l4 = new DataColumn("视力", Type.GetType("System.Int64"));
            DataColumn l5 = new DataColumn("创建时间", Type.GetType("System.DateTime"));
            DataColumn[] col = { l0, l1, l2, l3, l4, l5, };
            dt.Columns.AddRange(col);
            for (int i = 0; i < 5; i++)
            {
                DataRow row = dt.NewRow(); //new DataRow();
                row["编号"] = i + 1;
                row["姓名"] = "小王";
                row["年龄"] = 18;
                row["身高"] = 1516;
                row["视力"] = 15659;
                row["创建时间"] = DateTime.Now.ToLocalTime();
                dt.Rows.Add(row);
            }
            Response.Write(DateTableToJson(dt));
            //  [
            //  {"编号":1,"姓名":"小王","年龄":18,"身高":1516,"视力":15659,"创建时间":"\/Date(1443362884990)\/"},
            //  {"编号":2,"姓名":"小王","年龄":18,"身高":1516,"视力":15659,"创建时间":"\/Date(1443362884990)\/"},
            //  {"编号":3,"姓名":"小王","年龄":18,"身高":1516,"视力":15659,"创建时间":"\/Date(1443362884990)\/"},
            //  {"编号":4,"姓名":"小王","年龄":18,"身高":1516,"视力":15659,"创建时间":"\/Date(1443362884990)\/"},
            //  {"编号":5,"姓名":"小王","年龄":18,"身高":1516,"视力":15659,"创建时间":"\/Date(1443362884990)\/"}
            //  ]

  

 

  

  

posted on 2015-09-27 22:28  李可在江湖  阅读(1760)  评论(0编辑  收藏  举报