相关疑难解决方法(0)

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

是否可以排除某些字段包含在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的字符串.

javascript json

81
推荐指数
5
解决办法
6万
查看次数

JSON Stringfy只有一些属性

我有这个对象:

{
    name: "",            
    email: "",
    phone: "",
    protocol: "",
    area: "",
    subject: "",
    message: "",
    validation: this.validator.valid()
}
Run Code Online (Sandbox Code Playgroud)

我想将其转换为JSON,但是我不想要该validation属性。

我已经尝试了以下方法:

const test = JSON.stringify(this.state);
delete test.validation;
Run Code Online (Sandbox Code Playgroud)

还有其他方法吗?

javascript json

1
推荐指数
1
解决办法
80
查看次数

标签 统计

javascript ×2

json ×2