json序列化字符串并反序列调用的过程
2011-06-08 16:07
531 查看
1.首先json必须使用js的库文件,这里使用了<script src="/Content/scripts/cart/json2.js" type="text/javascript"></script>这个库文件
2.定义json的结构
2.json序列化字符串
从页面将字符串信息传到服务端,过程使用使用js
3.到达服务端后,存入cookie,方法如下
4.服务端调用时的反序列化方法:
5.逻辑中调用的并将数据返回到页面是使用
2.定义json的结构
[DataContract] public class CheckoutObj { [DataMember(Order = 0)] public string paymentCategory { get; set; } [DataMember(Order = 1)] public string paymentCustomId { get; set; } [DataMember(Order = 2)] public string receiverAddressId { get; set; } [DataMember(Order = 3)] public string deliveryTypeValue { get; set; } [DataMember(Order = 4)] public string zitiLocationValue { get; set; } }
2.json序列化字符串
从页面将字符串信息传到服务端,过程使用使用js
var m_obj = { receiverAddressId: $("#submitContent").attr("receiverAddressId"), deliveryTypeValue: $("#submitContent").attr("deliveryTypeValue"), zitiLocationValue: $("#submitContent").attr("zitiLocationValue"), paymentCategory: $("#submitContent").attr("paymentCategory"), paymentCustomId: $("#submitContent").attr("paymentCustomId") }; var jsonStr = JSON.stringify(m_obj); //用Json2.js生成Json字符串 $.ajax({ url: "/SimpleCheckOut/CreateRadioCookie", type: 'POST', data: { postjson: jsonStr }, dataType: 'json', error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus) }, success: function (result) { alert(result.paymentCategory) }
3.到达服务端后,存入cookie,方法如下
public ActionResult CreateRadioCookie() { VerifyUser vu = new VerifyUser(); var serializer = new DataContractJsonSerializer(typeof(CheckoutObj)); string dataString = Request["postjson"]; string newDataString = string.Empty; if (dataString != null) { var mStream = new MemoryStream(Encoding.UTF8.GetBytes(dataString)); CheckoutObj qry = (CheckoutObj)serializer.ReadObject(mStream); var stream = new MemoryStream(); serializer.WriteObject(stream, qry); byte[] dataBytes = new byte[stream.Length]; stream.Position = 0; stream.Read(dataBytes, 0, (int)stream.Length); newDataString = Encoding.UTF8.GetString(dataBytes); System.Web.HttpCookie mycookie = new HttpCookie(RadioCookieName+"-"+vu.CurrentUser.Userid); mycookie.Expires = DateTime.Now.AddDays(1); mycookie.Values.Add(RadioCookieName+"-"+vu.CurrentUser.Userid, HttpUtility.UrlEncode(newDataString)); System.Web.HttpContext.Current.Response.Cookies.Add(mycookie); } return Content(newDataString); }
4.服务端调用时的反序列化方法:
/// <summary> /// 获得json中的参数 /// </summary> /// <returns></returns> public CheckoutObj GetCheckoutObj() { VerifyUser vu = new VerifyUser(); HttpCookie oCookie = Request.Cookies[RadioCookieName+"-"+vu.CurrentUser.Userid]; if (oCookie == null) { CheckoutObj qry = new CheckoutObj() { paymentCategory = "", paymentCustomId = "", receiverAddressId = "", deliveryTypeValue = "", zitiLocationValue = "", }; return qry; } else { //Deserialize cookie string to CheckoutObj object string dataString = HttpUtility.UrlDecode(oCookie.Value); string ds = dataString.Substring(dataString.IndexOf("{")); var mStream = new MemoryStream(Encoding.UTF8.GetBytes(ds)); var serializer = new DataContractJsonSerializer(typeof(CheckoutObj)); CheckoutObj qry = (CheckoutObj)serializer.ReadObject(mStream); return qry; } }
5.逻辑中调用的并将数据返回到页面是使用
ViewData["checkoutObj"] = GetCheckoutObj();
相关文章推荐
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
- 基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service
- Web前台传对象字符串到后台并让后台反序列化对象字符串的方法(ASP.NET) json对象和字符串的转换
- Newtonsoft.Json序列化和反序列
- ASP.NET中把对象序列化成JSON字符串的三种方法
- 用Json.NET将json字符串反序列化为json匿名对象
- golang通过反射使用json字符串调用struct的指定方法及返回json结果
- com.google.gson json字符串的序列化与反序列化
- Http Post发送json序列请求(json序列化和反序列化)
- 数据库,序列化数据为json字符串
- json字符串与json对象、json序列化与反序列化
- 反序列化JSON字符串
- C# JSON字符串序列化与反序列化常见模型举例
- C#下JSON字符串的反序列化
- MVC MVVM Knockout viewmodel 提交 完整过程,包含序列化 JSON 和 字典模型绑定
- 此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
- Java http协议调用接口返回json字符串
- 编写一个函数escape(s,t),将字符串t复制到字符串s中,并在复制过程中将换行符、制表符等不可见字符分别转换为\n、\t等相应的可见的转义字符序列。要求使用switch语句。
- json工具性能比较:json-lib和jackson进行Java对象到json字符串序列化[转]
- iOS调用js方法传JSon字符串