是否可以排除某些字段包含在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的字符串.
我有这门课:
export class TblColabAdmin {
snomatrcompl: string;
nflativo: number;
ativo: boolean;
}
Run Code Online (Sandbox Code Playgroud)
我的Web服务实体中不存在属性ativo,因此我想避免将其添加到JSON中.
例如,在Java中,我们有@JsonIgnore注释.在TypeScript中是否存在类似的东西?