I need to put this json
"id": "x1",
"severity": 100,
"timestamp": 1599230966,
"attr1": "...",
"attr2": "...",
"origin": [{
"id": "x3",
"severity": 75,
"timestamp": 1599230366,
"attr1": "...",
"origin": [{
"id": "x2",
"severity": 50,
"timestamp": 1599229766,
"attr1": "...",
"attr2": 555,
"attr3": "...",
"origin": []
},
{
"id": "x1",
"severity": 25,
"timestamp": 1599229166,
"attr1": "...",
"origin": []
}
]
}]
into an object, and I just don't know how to define the object that is suitable for this json. I've struggled a lot,so please help me. Thank you in advance
You need to create an interface and assign it to the result.
export interface Origin{
id?: string;
severity?: number;
timestamp?: number;
attr1?: string;
attr2?: string;
orginArray: Origin[];
}
in you controller
private origin: Origin;
and in function of return the result
this.origin = result;
// Second option
//main.ts
export interface IOrigin {
id?: string;
severity?: number;
timestamp?: number;
attr1?: string;
attr2?: string;
origin: IOrigin[];
}
class Origin implements IOrigin {
id?: string;
severity?: number;
timestamp?: number;
attr1?: string;
attr2?: string;
origin: Origin[];
constructor(values: IOrigin) {
this.id = values.id;
// assign all your attributes
this.origin = values.origin !== undefined ? toOriginList(values.origin) : undefined;
}
}
function toOriginList(values: IOrigin[]): Origin[] {
return values.map(tree => new Origin(tree));
}
Hope useful