How to parse a JSON object to a TypeScript Object

后端 未结 8 729
谎友^
谎友^ 2020-11-30 05:21

I am currently trying to convert my received JSON Object into a TypeScript class with the same attributes and I cannot get it to work. What am I doing wrong?

8条回答
  •  猫巷女王i
    2020-11-30 05:52

    First of all you need to be sure that all attributes of that comes from the service are named the same in your class. Then you can parse the object and after that assign it to your new variable, something like this:

    const parsedJSON = JSON.parse(serverResponse);
    const employeeObj: Employee = parsedJSON as Employee;
    

    Try that!

提交回复
热议问题