asp.net序列化json三种方式 jquery ajax json

  装载: http://dev.21tx.com/2009/03/11/13975_1.html

 1 using System;

 2 using System.Collections;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Security;
 8 using System.Web.UI;
 9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.XML.Linq;
13 using System.Runtime.Serialization.Json;
14 using System.ServiceModel.Dispatcher;
15 using System.Text;
16 using System.Web.Script.Serialization;
17 using System.IO;
18 using System.Runtime.Serialization;
19 namespace WebDemo.AjaxTest
20 {
21     public partial class Process : System.Web.UI.Page
22     {
23         protected void Page_Load(object sender, EventArgs e)
24         {
25             if (!Request.Params.AllKeys.Contains("JsonStr"))
26             {
27                 GetData();
28             }
29             else
30             {
31                 LoadData();
32             }
33         }
34         /// <summary>
35         /// 从客户端加载数据
36         /// </summary>
37         private void LoadData()
38         {
39             string str = Request.Params["JsonStr"];
40             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.User));
41             MemoryStream ms=new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str.ToCharArray()));
42             Model.User user = (Model.User)serializer.ReadObject(ms);
43             ms.Close();
44         }
45         /// <summary>
46         /// 向客户端发送数据
47         /// </summary>
48         private void GetData()
49         {
50             Model.User user = new Model.User();
51             user.Address = "上海市闵行区";
52             user.Age = 26;
53             user.Name = "Windstore";
54             string resultStr;
55             //方式一:System.Runtime.Serialization.Json Assembly:System.ServiceModel.Web
56             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.User));
57             MemoryStream stream = new MemoryStream();
58             serializer.WriteObject(stream, user);
59             stream.Position = 0;
60             StreamReader sr = new StreamReader(stream);
61             resultStr = sr.ReadToEnd();
62             sr.Close();
63             stream.Close();
64             //方式二:System.ServiceModel.Dispatcher.JsonQueryStringConverter Assembly:System.ServiceModel
65             //JsonQueryStringConverter convert = new JsonQueryStringConverter();
66             //resultStr = convert.ConvertValueToString(user, typeof(Model.User));
67             //方式三:System.Web.Script.Serialization
68             //StringBuilder sb = new StringBuilder();
69             //JavaScriptSerializer jsServilizer = new JavaScriptSerializer();
70             //jsServilizer.Serialize(user, sb);
71             //resultStr = sb.ToString();
72             Response.ContentType = "application/json";
73             Response.Write(resultStr);
74             Response.Flush();
75             Response.End();
76         }
77     }
78 }
79

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值