是否可以排除某些字段包含在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的字符串.
我有这个对象:
{
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)
还有其他方法吗?