隐藏JSON.stringify()输出中的某些值

Nil*_*esh 81 javascript json

是否可以排除某些字段包含在json字符串中?

这是一些伪代码

var x = {
    x:0,
    y:0,
    divID:"xyz",
    privateProperty1: 'foo',
    privateProperty2: 'bar'
}
Run Code Online (Sandbox Code Playgroud)

我想排除privateProperty1和privateproperty2出现在json字符串中

所以我想,我可以使用stringify替换器功能

function replacer(key,value)
{
    if (key=="privateProperty1") then retun "none";
    else if (key=="privateProperty2") then retun "none";
    else return value;
}
Run Code Online (Sandbox Code Playgroud)

并在stringify中

var jsonString = json.stringify(x,replacer);
Run Code Online (Sandbox Code Playgroud)

但是在jsonString中我仍然看到它

{...privateProperty1:value..., privateProperty2:value }
Run Code Online (Sandbox Code Playgroud)

我想要没有privateproperties的字符串.

Jar*_*ish 92

Mozilla的文档说返回undefined(代替"none"):

http://jsfiddle.net/userdude/rZ5Px/

function replacer(key,value)
{
    if (key=="privateProperty1") return undefined;
    else if (key=="privateProperty2") return undefined;
    else return value;
}

var x = {
    x:0,
    y:0,
    divID:"xyz",
    privateProperty1: 'foo',
    privateProperty2: 'bar'
};

alert(JSON.stringify(x, replacer));
Run Code Online (Sandbox Code Playgroud)

这是一个复制方法,以防您决定走这条路线(根据您的评论).

http://jsfiddle.net/userdude/644sJ/

function omitKeys(obj, keys)
{
    var dup = {};
    for (var key in obj) {
        if (keys.indexOf(key) == -1) {
            dup[key] = obj[key];
        }
    }
    return dup;
}

var x = {
    x:0,
    y:0,
    divID:"xyz",
    privateProperty1: 'foo',
    privateProperty2: 'bar'
};

alert(JSON.stringify(omitKeys(x, ['privateProperty1','privateProperty2'])));
Run Code Online (Sandbox Code Playgroud)

编辑 - 我更改了底部功能中的功能键,以防止混淆.


Cur*_*lop 27

另一个好的解决方案:(需要下划线)

x.toJSON = function () {
    return _.omit(this, [ "privateProperty1", "privateProperty2" ]);
};
Run Code Online (Sandbox Code Playgroud)

这个解决方案的好处是任何在x上调用JSON.stringify的人都会得到正确的结果 - 你不必单独改变JSON.stringify调用.

非下划线版本:

x.toJSON = function () {
    var result = {};
    for (var x in this) {
        if (x !== "privateProperty1" && x !== "privateProperty2") {
            result[x] = this[x];
        }
    }
    return result;
};
Run Code Online (Sandbox Code Playgroud)

  • 重写toJSON是执行此操作的正确方法 (5认同)

小智 15

您可以使用Object中的本机函数defineProperty:

var data = {a: 10};
Object.defineProperty(data, 'transient', {value: 'static', writable: true});
data.transient = 'dasda';
console.log(JSON.stringify(data)); //{"a":10}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案有效,因为此属性描述符的`enumerable`值为false. (6认同)

del*_*elp 15

这是一个老问题,但我添加了一个答案,因为有一种更简单的方法来处理这个问题。传递您希望在 JSON 中输出的字符串数组。

var x = {
    x:0,
    y:0,
    divID:"xyz",
    privateProperty1: 'foo',
    privateProperty2: 'bar'
}

JSON.stringify(x, ["x", "y", "divID"]);

// This will output only x y and divID
// {"x":0,"y":0,"divID":"xyz"}
Run Code Online (Sandbox Code Playgroud)


Rei*_*ard 10

这是我使用展开运算符 ( ...) 的方法:

const obj = {
  name: "hello",
  age: 42,
  id: "3942"
};

const objWithoutId = { ...obj, id: undefined };

const jsonWithoutId = JSON.stringify(objWithoutId);

console.log(jsonWithoutId);
Run Code Online (Sandbox Code Playgroud)