How to parse JSON string in Typescript

后端 未结 7 1486
[愿得一人]
[愿得一人] 2020-12-04 18:29

Is there a way to parse strings as JSON in Typescript.
Example: In JS, we can use JSON.parse(). Is there a similar function in Typescript?

I have a

7条回答
  •  萌比男神i
    2020-12-04 19:16

    There is a great library for it ts-json-object

    In your case you would need to run the following code:

    import {JSONObject, required} from 'ts-json-object'
    
    class Response extends JSONObject {
        @required
        name: string;
    
        @required
        error: boolean;
    }
    
    let resp = new Response({"name": "Bob", "error": false});
    

    This library will validate the json before parsing

提交回复
热议问题