首页 > 解决方案 > 如何对打字稿中定义的代码进行 JSON 化

问题描述

有没有办法对打字稿中定义的代码进行 JSON 化?

export type ArticleList = ArticleListItem[];

export class ArticleListItem {
  title!: string;
  date?: string;
  key_type!: KeyTypes;
  path!: string;
  query: string = '';

  constructor( item: ArticleListItem ) {
    Object.assign( this, item );
  }
}

我想将上面的打字稿代码更改为 JSON。

标签: javascriptjsontypescript

解决方案


您可以使用JSON.stringify(new ArticleListitem('x'))方法来获取json。参考 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify


推荐阅读