Angular2将嵌套的json对象强制转换为类型

时间:2017-01-04 08:54:44

标签: json angular

我正在使用Angular2和打字稿。 WebApi返回JSON,我想转换成某种类型。

这是我的类型:

export interface Content {
  title: string;
  description: string;
} 

web api返回以下JSON字符串。

[
  {
    "contentId": "123",
    "contentJson": {
      "title": "ABC",
      "description": "ABC Description"
    },
    "cultureName": "en-us"
  },
  {
    "contentId": "124",
    "contentJson": {
      "title": "DEF",
      "description": "DEF Description"
    },
    "cultureName": "en-us"
  },
  {
    "contentId": "222",
    "contentJson": {
      "title": "ZZZ",
      "description": "ZZZ Description"
    },
    "cultureName": "en-us"
  },
  {
    "contentId": "125",
    "contentJson": {
      "title": "AAA",
      "description": "AAA Description"
    },
    "cultureName": "en-us"
  }
]

我所需的类型在contentJson对象中。我如何投射Angular 2?

1 个答案:

答案 0 :(得分:0)

使用<Type>进行演员表 试试这个

var content = <Content>yourJsonArray[i].contentJson