小编Mor*_*ard的帖子

如何JSONify javascript对象的属性

我正在通过我的javascript类'属性进行"手动"运行以形成JSON,如下所示.它感觉很笨拙,我会自动学习如何做到这一点,所以如果我添加或删除任何属性,我就不必乱用'toJson'函数.

一个有用的思想可以指出我如何调整以下'toJson'功能朝着这个目的正确的方向吗?

提前谢谢了.

/* Using Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.*/
var LogEntry = Class.extend({
    init: function (_conferenceId, _tokenId, _logType, _logValue) {
        this.dato = new Date();
        this.logValue = _logValue;
        this.logType = _logType;
        this.conferenceId = _conferenceId;
        this.tokenId = _tokenId;
    },
    toJson: function () {
        // ?
        var jsonStringBuilder = '{ ';
        jsonStringBuilder += '"dato": ' + this.dato.toString() + ',';
        jsonStringBuilder += '"conferenceId": ' + this.conferenceId + ',';
        if (this.tokenId== null) {
            jsonStringBuilder += '"tokenId":null,';
        }
        else …
Run Code Online (Sandbox Code Playgroud)

javascript json

28
推荐指数
2
解决办法
6万
查看次数

标签 统计

javascript ×1

json ×1