我的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