序列化javascript对象并对其进行反序列化

serialize javascript object and deserialize it

本文关键字:反序列化 javascript 对象 序列化      更新时间:2023-09-26

我有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" />

我想要实现的目标

  1. 通过序列化将contextObj分配给asp:hidden元素(可以是json格式)
  2. 在Code-behind中,让这个对象去跟踪它,并将值分配给它各自的c#类,即CustomerDTO
  3. 这样我就可以通过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访问变量,并且还可以使用私有成员序列化对象!我认为这是一个双赢的局面!

Javascript序列化程序不像C#那样使用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');