序列化javascript对象并对其进行反序列化
serialize javascript object and deserialize it
我有javascript getter setter类
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
点击事件中创建此类对象
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格式) - 在Code-behind中,让这个对象去跟踪它,并将值分配给它各自的c#类,即
CustomerDTO
- 这样我就可以通过out all pages请求访问所有这些值(通过在Server.Transfer请求中传递此对象)
为了序列化对象,我尝试了这个
console.log(JSON.stringify(contextObj));
但它什么也没印。我想打印值,这样我就可以分配给隐藏变量
您有错误的语法。问题是您的变量是私有的,stringify()函数无法访问它们。事实上,如果你试图直接访问一个私有变量,你会得到"未定义"。
序列化只对公共成员有效,这是有意义的,因为私有变量是不可访问的。
这应该是一个快速破解,但它会公开您的变量(只要您使用This.category_id=value),因此它破坏了封装,这是不好的:
function UserContext() {
this.category_id;
this.biller_id;
this.get_category_id = function() {
return this.category_id;
}
this.set_category_id = function(value) {
this.category_id = value;
}
this.get_biller_id = function() {
return this.biller_id;
}
this.set_biller_id = function(value) {
this.biller_id = value;
}
}
var contextObj = new UserContext();
contextObj.set_category_id('SOME VALUE');
contextObj.set_biller_id('65');
alert(JSON.stringify(contextObj));
更好的方法是保持category_id和biller_id真正私有,并且仍然能够序列化对象。通过在对象中实现ToJson()方法,并显式指定要序列化的内容,可以做到这一点: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;
}
this.toJSON = function() {
return {
"category_id": category_id,
"biller_id": biller_id
};
};
}
var contextObj = new UserContext();
contextObj.set_category_id('SOME VALUE');
contextObj.set_biller_id('65');
alert(JSON.stringify(contextObj));
因此,只能通过setter和getter访问变量,并且还可以使用私有成员序列化对象!我认为这是一个双赢的局面!
function UserContext() {
this.category_id = null;
this.biller_id = null;
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;
}
}
那么它应该正确地将属性添加到json:中
var contextObj = new UserContext();
console.log(JSON.stringify(contextObj ));
将代码更改为:
function UserContext() {
var category_id;
var biller_id;
this.get_category_id = function () {
return this.category_id;
}
this.set_category_id = function (value) {
this.category_id = value;
}
this.get_biller_id = function () {
return this.biller_id;
}
this.set_biller_id = function (value) {
this.biller_id = value;
}
}
不要按照其他人的建议这样做:
this.category_id = "";
this.biller_id = "";
这些都是私人的。保持他们那样。
I think this way of function definition looks fine
function UserContext() {
var category_id = "";
var biller_id = "";
this.get_category_id = function() {
return this.category_id;
}
this.set_category_id = function(value) {
this.category_id = value;
}
this.get_biller_id = function() {
return this.biller_id;
}
alert(JSON.stringify(contextObj));//now check in alert for out put
this.set_biller_id = function(value) {
this.biller_id = value;
}
}
var contextObj = new UserContext();
contextObj.set_category_id('Delhi');
contextObj.set_biller_id('43');
相关文章:
- 是否可以在javascript中反序列化java对象
- 使用 JavaScript 进行反序列化
- 如何:从javascript反序列化C#对象
- 在 javascript 中序列化和反序列化数组
- Javascript:反序列化对象和混合方法
- JavaScript 中的 JSON 反序列化
- 反序列化 JavaScript 对象实例
- 无法在 Javascript 中反序列化 JSON 对象
- 在浏览器或节点应用程序中的 JavaScript 中反序列化 UTF8 编码的 Byte[].js
- 如何在 JavaScript 中反序列化日期时间
- Javascript;Json:从序列化和反序列化中排除字段
- javascript中的JSON序列化和php反序列化
- 序列化javascript对象并对其进行反序列化
- 如何反序列化未知派生类的JavaScript对象
- JavaScript等价于.net XML反序列化
- c# Web服务到javascript反序列化JSON对象
- javascript中的JSON反序列化
- PHP反序列化Javascript序列化字符串
- 从反序列化Javascript中重命名属性
- Web API可以't反序列化Javascript Date.toISOString()