创建一个类库,名为BLL,代码如下
Code
1using System;
2
3// 添加引用 System.ServiceModel.Web、System.Runtime.Serialization
4// 再引入以下两个命名空间
5using System.Runtime.Serialization;
6using System.Runtime.Serialization.Json;
7
8namespace BLL
9{
10 [DataContract]
11 public class jsonTest
12 {
13 public jsonTest() { }
14 public jsonTest(string firstname, string lastname)
15 {
16 this.FirstName = firstname;
17 this.LastName = lastname;
18 }
19
20 [DataMember]
21 public string FirstName { get; set; }
22
23 [DataMember]
24 public string LastName { get; set; }
25 }
26}
27
1using System;
2
3// 添加引用 System.ServiceModel.Web、System.Runtime.Serialization
4// 再引入以下两个命名空间
5using System.Runtime.Serialization;
6using System.Runtime.Serialization.Json;
7
8namespace BLL
9{
10 [DataContract]
11 public class jsonTest
12 {
13 public jsonTest() { }
14 public jsonTest(string firstname, string lastname)
15 {
16 this.FirstName = firstname;
17 this.LastName = lastname;
18 }
19
20 [DataMember]
21 public string FirstName { get; set; }
22
23 [DataMember]
24 public string LastName { get; set; }
25 }
26}
27
在项目中添加对上面这个类库的引用,
在项目中添加引用 System.ServiceModel.Web、System.Runtime.Serialization
Code
1using System;
2using System.Web;
3
4// 引入以下命名空间
5using System.Runtime.Serialization;
6using System.Runtime.Serialization.Json;
7using System.IO;
8using System.Text;
9
10using BLL;
11namespace WebTest
12{
13 public partial class JSON : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 jsonTest j = new jsonTest("long", "huang");
18
19 System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(j.GetType());
20
21
22 // 直接将对象序列化到输出流
23 //MemoryStream mStream = new MemoryStream();
24
25 //serializer.WriteObject(HttpContext.Current.Response.OutputStream, j);
26
27 //string jsonStr = Encoding.Default.GetString(mStream.ToArray());
28
29 //Response.End();
30
31
32
33 // 先将对象序列化到内存流中,再输出
34 MemoryStream mStream = new MemoryStream();
35
36 serializer.WriteObject(mStream, j);
37
38 string jsonStr = Encoding.Default.GetString(mStream.ToArray());
39
40 Response.Write(jsonStr);
41 }
42 }
43}
44
1using System;
2using System.Web;
3
4// 引入以下命名空间
5using System.Runtime.Serialization;
6using System.Runtime.Serialization.Json;
7using System.IO;
8using System.Text;
9
10using BLL;
11namespace WebTest
12{
13 public partial class JSON : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 jsonTest j = new jsonTest("long", "huang");
18
19 System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(j.GetType());
20
21
22 // 直接将对象序列化到输出流
23 //MemoryStream mStream = new MemoryStream();
24
25 //serializer.WriteObject(HttpContext.Current.Response.OutputStream, j);
26
27 //string jsonStr = Encoding.Default.GetString(mStream.ToArray());
28
29 //Response.End();
30
31
32
33 // 先将对象序列化到内存流中,再输出
34 MemoryStream mStream = new MemoryStream();
35
36 serializer.WriteObject(mStream, j);
37
38 string jsonStr = Encoding.Default.GetString(mStream.ToArray());
39
40 Response.Write(jsonStr);
41 }
42 }
43}
44