How do I initialize a TypeScript object with a JSON object

前端 未结 16 909
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 08:30

I receive a JSON object from an AJAX call to a REST server. This object has property names that match my TypeScript class (this is a follow-on to this question).

Wha

16条回答
  •  庸人自扰
    2020-11-22 09:16

    Maybe not actual, but simple solution:

    interface Bar{
    x:number;
    y?:string; 
    }
    
    var baz:Bar = JSON.parse(jsonString);
    alert(baz.y);
    

    work for difficult dependencies too!!!

提交回复
热议问题