JSON 字符串化对象(不包括方法)

发布于 2024-12-02 16:44:54 字数 1203 浏览 1 评论 0原文

我正在开发 Firefox 扩展,并尝试对 JSON 对象进行字符串化。

我正在使用这个 stringify 函数 但我收到此错误:

无法转换 JavaScript 参数“NS_ERROR_XPC_BAD_CONVERT_JS”

我真的只关心对象内的第一级或第二级或属性,而不关心方法/函数。如果我不需要所有这些,是否有更简单的方法来字符串化对象?

这是我正在使用的代码:

    var s=JSONstring.make('abc');

    try{

        Firebug.Console.log(gContextMenu);

        s = JSON.stringify(gContextMenu);

        Firebug.Console.log(s);
    }catch(e){
        Firebug.Console.log('error');
        Firebug.Console.log(e);
    }
    var s=JSONstring.make('abc');
    Firebug.Console.log(s);
    Firebug.Console.log(gContextMenu);

这是控制台窗口中的错误:

image

这是我能够从 Firebug 控制台窗口中复制出来的内容:

http://pastebin.com/KPXceRag

这是该对象的屏幕截图:

图片 http://img143.imageshack.us/img143/2603/pictureos.png

I'm working on a Firefox extension and I'm trying to stringify a JSON object.

I'm using this stringify function but I'm getting this error:

Could not convert JavaScript argument "NS_ERROR_XPC_BAD_CONVERT_JS"

I really just care about the first level or two or properties inside the object, and I don't care about the methods / functions. Is there a simpler way to stringify an object if I don't need all this?

Here's the bit of code I'm using:

    var s=JSONstring.make('abc');

    try{

        Firebug.Console.log(gContextMenu);

        s = JSON.stringify(gContextMenu);

        Firebug.Console.log(s);
    }catch(e){
        Firebug.Console.log('error');
        Firebug.Console.log(e);
    }
    var s=JSONstring.make('abc');
    Firebug.Console.log(s);
    Firebug.Console.log(gContextMenu);

Here is the error in the console window:

image

This is what I was able to copy out of the Firebug console window:

http://pastebin.com/KPXceRag

Here is a screenshot of the object:

image http://img143.imageshack.us/img143/2603/pictureos.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-12-09 16:44:54

您可以在对象上定义一个名为 toJSON() 的自定义函数,该函数仅返回所需对象的元素。有点违反直觉的是,您的 toJSON 函数不应该返回 JSON 字符串 - 它只返回一个对象,表示应用作 JSON.stringify< 的输入的内容/代码>。

例如:

// an object with some attributes you want and some you don't
var o = {
    a:"value1", 
    b:"value2", 
    doCalc: function() { return this.a + " " +  this.b } 
};
// define a custom toJSON() method
o.toJSON = function() { 
    return {
        a:this.a, 
        calc: this.doCalc() 
    } 
};

JSON.stringify(o); // '{"a":"value","calc":"value1 value2"}'

在您的情况下,您应该能够在调用 JSON.stringify 之前为 gContextMenu 动态定义此方法。这确实需要你明确定义你想要什么和不想要什么,但我认为没有更好的方法。

编辑:如果您想提取所有非方法值,您可以尝试如下操作:

o.toJSON = function() { 
    var attrs = {};
    for (var attr in this) {
        if (typeof this[attr] != "function") {
            attrs[attr] = String(this[attr]); // force to string
        }
    }
    return attrs;
};

您可能最终会将一些属性设置为“[object Object]”,但如果您想要的只是检查,这可能不是问题。您还可以尝试检查 typeof this[attr] == "string" || typeof this[attr] == "number" 如果您只想获取这些类型的属性。

You can define a custom function on your object called toJSON() that returns only the elements of the object you want. Somewhat counter-intuitively, your toJSON function should not return a JSON string - it just returns an object representing what should be used as the input for JSON.stringify.

For example:

// an object with some attributes you want and some you don't
var o = {
    a:"value1", 
    b:"value2", 
    doCalc: function() { return this.a + " " +  this.b } 
};
// define a custom toJSON() method
o.toJSON = function() { 
    return {
        a:this.a, 
        calc: this.doCalc() 
    } 
};

JSON.stringify(o); // '{"a":"value","calc":"value1 value2"}'

In your case, you should be able to define this method for gContextMenu on the fly, before calling JSON.stringify. This does require you to explicitly define what you want and don't want, but I don't think there's a better way.

Edit: If you want to pull all non-method values, you could try something like this:

o.toJSON = function() { 
    var attrs = {};
    for (var attr in this) {
        if (typeof this[attr] != "function") {
            attrs[attr] = String(this[attr]); // force to string
        }
    }
    return attrs;
};

You will probably end up with a few attributes set to "[object Object]", but if all you want is inspection, this probably isn't an issue. You could also try checking for typeof this[attr] == "string" || typeof this[attr] == "number" if you wanted to get only those types of attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文