如何将接口包装在另一个JSON中



目前我有一个定义为的接口

export interface IBlog {
    id?: number;
    title: string;
    body: string;
    type: number;
}

这将呈现一些json,看起来像这个

{
 "title":"What the hell",
 "body":"A body a body",
 "type":1
}

然而,我的后端正在期待类似的东西

  {
   "blog":{
    "title":"What the hell",
    "body":"A body a body",
    "type":1        
   }
  }

我正在使用JSON.stringify()来渲染json,我想知道是否可以获得外部json包装。如果可能的话,我想在不将其包装在外部接口的情况下完成

我不确定你是在问如何在typescript中表示这一点,还是在问如何创建一个具有这种结构的js对象。

如果是第一个,那么:

interface IBlog {
    id?: number;
    title: string;
    body: string;
    type: number;
}
interface IRequest {
    blog: IBlog;
}

如果第二个则:

let blog: IBlog = ...
let request: IRequest = { blog: blog };

最新更新