我的JavaScript的getter setter方法类序列化JavaScript对象和反序列化
function UserContext() {
var category_id;
var biller_id;
this.get_category_id = function() {
return category_id;
}
this.set_category_id = function (value) {
category_id = value;
}
this.get_biller_id = function() {
return biller_id;
}
this.set_biller_id = function (value) {
biller_id = value;
}
}
我在jquery
click事件创建该类的对象
var contextObj = new UserContext();
contextObj.set_category_id('SOME VALUE');
contextObj.set_biller_id('65');
我也有类似的类在C#
public class CustomerDTO
{
public string category_id { get; set; }
public string biller_id{ get; set; }
}
和一个asp:hidden
元件
<asp:HiddenField ID="hdnValue" ClientIDMode="Static" runat="server" />
我想通过序列化实现
- 分配
contextObj
到asp:hidden
元素(可以是JSON格式)什么 - 在后面的代码得到这个对象desrailize它并分配值的相应的c#类,即
CustomerDTO
- 因此,我可以通过所有页面访问所有这些值请求(通过在Server.Transfer请求中传递此对象)
要序列化对象我想这
console.log(JSON.stringify(contextObj));
但不打印输出。我希望打印值,以便我可以分配给隐藏变量
尝试使用'$ .parseJSON()'http://api.jquery.com/jquery.parsejson/ – 2014-09-26 07:52:28
@ZeeTee为什么? – 2014-09-26 07:53:08
序列化时,函数不用于获取值。 您的getter和setter在JavaScript中需要暴露在_class_中。 尝试用关键字this声明你的变量。 – Cyrbil 2014-09-26 07:53:22