我正在通过我的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 {
jsonStringBuilder += '"tokenId": ' + _tokenId + ',';
}
jsonStringBuilder += '"logValue": ' + this.logValue + ',';
jsonStringBuilder += '"logType": ' + this.logType;
jsonStringBuilder += '}';
return jsonStringBuilder;
}
});
JSON.stringify
是您要寻找的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句