如何 JSONify 一个 javascript 对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19234547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:52:06  来源:igfitidea点击:

How to JSONify a javascript object's properties

javascriptjson

提问by Morten N?rgaard

I'm doing a 'manual' run through my javascript class' properties in order to form JSON, as seen below. It feels clumsy and I would to learn how to do this automatically, so I wouldn't have to mess with the 'toJson' function if I add or remove any properties, for example.

我正在通过我的 javascript 类的属性进行“手动”运行,以形成 JSON,如下所示。感觉很笨拙,我会学习如何自动执行此操作,因此,例如,如果我添加或删除任何属性,我就不必弄乱“toJson”函数。

Can a helpful mind point me in the right direction on how to adapt the below 'toJson' function towards this purpose?

一个有用的想法能否为我指明如何将下面的“toJson”函数用于此目的的正确方向?

Many thanks in advance.

提前谢谢了。

/* 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;
        }
});

回答by josh3736

JSON.stringifyis the function you're looking for.

JSON.stringify是您正在寻找的功能。

Some very old browsersdo not natively provide the JSONobject, but you can use a shim libraryfor those browsers.

一些非常旧的浏览器本身不提供该JSON对象,但您可以为这些浏览器使用shim 库

回答by GolezTrol

I think you're looking for JSON.stringify().

我想你正在寻找JSON.stringify().

回答by Mike H.

You can use stringify if you want. Javascript is also really freakin' cool and allows you to pass context abstracts so you really don't need to define class members as you are. I don't know what your end goal is but check this:

如果需要,您可以使用 stringify。Javascript 也非常酷,它允许您传递上下文抽象,因此您实际上不需要按原样定义类成员。我不知道你的最终目标是什么,但检查一下:

function foo(context) { 
    dosomething(context.bar);
}

You can dynamically add an arbitrary number of members with simple declarations, making context a JSON object inherently like this:

您可以使用简单的声明动态添加任意数量的成员,使上下文本质上是一个 JSON 对象,如下所示:

context.bar = "hello world";

This way when you pass 'context' to the server, there's no real need to "stringify" it (assuming your backend framework has some sort of JSON parsing that is).

这样,当您将“上下文”传递给服务器时,就没有真正需要对其进行“字符串化”(假设您的后端框架具有某种 JSON 解析)。

Edit: I got a little off topic, I just hate seeing long strings of params in JS

编辑:我有点跑题了,我只是讨厌在 JS 中看到长串的参数