简体   繁体   English

使用Typescript中的数组反序列化JSON对象

[英]Deserialize JSON Object with arrays in Typescript

I want to deserialize a JSON Object in Typescript . 我想在Typescript反序列化JSON对象。 I have found this relevant question I would like to use the approach 4 of the accepted answer. 我发现了这个相关问题,我想使用已接受答案的approach 4 However I am not sure if this would work in my case, since the object has member of arrays of other objects and so do the objects in the array as well. 但是我不知道这是否会在我的情况下工作,因为此对象的成员arrays其他对象等方面做了对象的array也是如此。 Moreover I would like to use a generic method/approach which deserializes the objects even if one does not know where the object dependencies end. 此外,我想使用一种通用的方法/方法来对对象进行反序列化,即使不知道对象依赖项在何处结束。 The object structure looks like: 对象结构如下:

class Parent {

s: string;
x: number;
children : Array<Child1>
...

} 

class Child1 {

t: string;
y: number;
children : Array<Child2>
...

}

class Child2 {

k: string;
z: number;
children : Array<Child3>;
...

}

...

How can I deserialize these type of objects? 如何反序列化这些类型的对象? I would be satisfied even with an approach which takes the end of the object structure for granted. 即使采用将对象结构的结尾视为理所当然的方法,我也会感到满意。

I'm not sure if I understand your full requirements, but the method that you say you want to use basically makes each class responsible for deserializing itself. 我不确定我是否理解您的全部要求,但是您说的要使用的方法基本上会使每个类负责反序列化。 So if parent knows it has a Child1 array, it knows it can iterate over the children array in the json and then call on Child1 to deserialize each child. 因此,如果parent知道它具有Child1数组,则知道可以迭代json中的children数组,然后调用Child1以反序列化每个孩子。 Child1 can then do the same for its children, and so on: 然后, Child1可以为其子代执行相同操作,依此类推:

class Parent {
    s: string;
    x:number;
    children: Child1[] = [];

    deserialize(input) {
        this.s = input.s;
        this.x = input.x;
        for(let child of input.children){
            this.children.push(new Child1().deserialize(child))
        }
        return this;
    }
}

class Child1{
    t: string;
    y: number;
    children: Child2[] = []
    deserialize(input) {
        this.t = input.t;
        this.y = input.x;
        for(let child of input.children){
            this.children.push(new Child2().deserialize(child))
        }

        return this;
    }
}

class Child2{
    deserialize(input) {
        //...
        return this;
    }

}

To avoid having to list out all the properties, I used this link . 为了避免列出所有属性,我使用了此链接

The premise is to have a deserializable interface which the classes implement: 前提是要有一个可反序列化的接口,这些类可以实现:

export interface Deserializable {
   deserialize(input: any): this;
}

We then make use of Object.assign. 然后,我们使用Object.assign。

Classes: 类别:

class Parent {
    s: string;
    x: number;
    children: Child1[] = [];

    deserialize(input) {
        Object.assign(this, input);
        let deserializedChildren: Child1[] = [];
        for(let child of input.children){
            deserializedChildren.push(new Child1().deserialize(child))
        }
        this.children = deserializedChildren;
        return this;
    }
}

class Child1{
    t: string;
    y: number;

    deserialize(input) {
        Object.assign(this, input);
        return this;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM