过去我们使用JSON.net序列化一个对象,在asp.net3.5中已经集成了序列化对象为json的方法。
我们分别针对System.Runtime.Serialization.Json和System.Web.Script.Serialization两个命名空间下的不同方法进行序列化和反序列化。
第一种方法:
System.Runtime.Serialization.Json
(在vs2008添加引用System.Runtime.Serialization 如果在非WEB项目下还需要引用System.ServiceModel.Web)
JosnHelper.CS 代码如下:
View Code
1 public class JsonHelper
2 {
3 /// <summary>
4 /// 生成Json格式
5 /// </summary>
6 /// <typeparam name="T"></typeparam>
7 /// <param name="obj"></param>
8 /// <returns></returns>
9 public static string GetJson<T>(T obj)
10 {
11 DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
12 using (MemoryStream stream = new MemoryStream())
13 {
14 json.WriteObject(stream, obj);
15 string szJson = Encoding.UTF8.GetString(stream.ToArray()); return szJson;
16 }
17 }
18 /// <summary>
19 /// 获取Json的Model
20 /// </summary>
21 /// <typeparam name="T"></typeparam>
22 /// <param name="szJson"></param>
23 /// <returns></returns>
24 public static T ParseFromJson<T>(string szJson)
25 {
26 T obj = Activator.CreateInstance<T>();
27 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
28 {
29 DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
30 return (T)serializer.ReadObject(ms);
31 }
32 }
33 }
下面为具体用法
声明一个需要输出JSON属性的类
View Code
1 public class topMenu
2 {
3 public string id { get; set; }
4 public string title { get; set; }
5 public string defaulturl { get; set; }
6 }
本例使它循环三次 利用上面GetJson方法序列化
View Code
1 topMenu t_menu = new topMenu()
2 {
3 id = "1",
4 title = "全局",
5 defaulturl = "123456"
6 };
7
8 List<topMenu> l_topmenu = new List<topMenu>();
9 for (int i = 0; i < 3; i++)
10 {
11 l_topmenu.Add(t_menu);
12 }
13 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
View Code
1 string szJson = @"{""id"":""1"",""title"":""全局"",""defaulturl"":""123456""} ";
2 topMenu t_menu2 = JsonHelper.ParseFromJson<topMenu>(szJson);
3 Response.Write(t_menu2.title);
输出结果为:全局
第二种方法
System.Web.Script.Serialization (引用System.Web.Extensions.dll)
还是用到上面方法中JSON属性的类
下面和上面方法中一样。循环三次就。序列化方式不一样
View Code
1 topMenu t_menu = new topMenu()
2 {
3 id = "1",
4 title = "全局",
5 defaulturl = "123456"
6 };
7
8 List<topMenu> l_topmenu = new List<topMenu>();
9
10 for (int i = 0; i < 3; i++)
11 {
12 l_topmenu.Add(t_menu);
13 }
下面用这种方式输出:
View Code
1 JavaScriptSerializer jss = new JavaScriptSerializer();
2 Response.Write( jss.Serialize(l_topmenu ));
输出结果是相同的
[{"defaulturl":"123456","id":"1","title":"全局"},
{"defaulturl":"123456","id":"1","title":"全局"},
{"defaulturl":"123456","id":"1","title":"全局"}]
下面利用JavaScriptSerializer中的Deserialize方法读取Json
View Code
1 string szJson = @"{""id"":""1"",""title"":""全局"",""defaulturl"":""123456""} ";
2 topMenu toptabmenu = jss.Deserialize<topMenu>(szJson);
3 Response.Write( jss.Serialize(toptabmenu.title));
输出结果为:全局
综上。两种方法个有好处。一个比较灵活。一个较简洁