隐藏 JSON.stringify() 输出中的某些值
2022-08-30 05:42:03
是否可以在 json 字符串中排除某些字段?
这里有一些伪代码
var x = {
x:0,
y:0,
divID:"xyz",
privateProperty1: 'foo',
privateProperty2: 'bar'
}
我想从json字符串中排除privateProperty1和privateproperty2
所以我想,我可以使用字符串化替换器函数
function replacer(key,value)
{
if (key=="privateProperty1") then retun "none";
else if (key=="privateProperty2") then retun "none";
else return value;
}
并在字符串化
var jsonString = json.stringify(x,replacer);
但是在jsonString中,我仍然将其视为
{...privateProperty1:value..., privateProperty2:value }
我想把绳子里没有。