您的位置:首页 > Web前端 > JavaScript

json序列化字符串并反序列调用的过程

2011-06-08 16:07 531 查看
1.首先json必须使用js的库文件,这里使用了<script src="/Content/scripts/cart/json2.js" type="text/javascript"></script>这个库文件

 

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();
 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐