JSON.stringify 不适用于 Typescript 中对象的所有属性

JSON.stringify not working for all properties of object in Typescript

JSON.stringify 不适用于 Typescript 中最外层对象的所有属性。 请参考以下代码片段:

class Criterion {
    '@CLASS' = 'xyz.abc.Criterion'
    operator: string;
    operand: string[];
    field: string;
    constructor(field:string,operator:string,operand: string[]) {
      this.field = field;
      this.operator = operator;
      this.operand = operand;
    }
}

class Filter {
    '@CLASS': 'xyz.ada.Filter';
    criteria: Criterion[];
    constructor(criteria:Criterion[]) {
        this.criteria=criteria;
    }
}

var criterion = new Criterion("a","b",["c"]);
var a = new Filter([criterion]);
JSON.stringify(a);
console.log(JSON.stringify(a));

输出:

{"criteria":[{"@CLASS":"xyz.abc.Criterion","field":"a","operator":"b","operand":["c"]}]}

预计:

{"@CLASS":"xyz.ada.Filter","criteria":[{"@CLASS":"xyz.abc.Criterion","field":"a","operator":"b","operand":["c"]}]}

它没有将过滤器 class 对象的 @CLASS 属性 字符串化。

code link

您的 class 过滤器中有错字。将两个点替换为:

'@CLASS'= 'xyz.ada.Filter';