Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2将嵌套json对象强制转换为类型_Json_Angular - Fatal编程技术网

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

Angular2将嵌套json对象强制转换为类型,json,angular,Json,Angular,我用的是Angular2和打字脚本。WebApi返回JSON,我想转换成某种类型 这是我的类型: 导出接口内容{ 标题:字符串; 描述:字符串; } web api返回以下JSON字符串 [ { “contentId”:“123”, “contentJson”:{ “标题”:“ABC”, “说明”:“ABC说明” }, “cultureName”:“en us” }, { “contentId”:“124”, “contentJson”:{ “标题”:“定义”, “说明”:“DEF说明” }

我用的是Angular2和打字脚本。WebApi返回JSON,我想转换成某种类型

这是我的类型:

导出接口内容{
标题:字符串;
描述:字符串;
} 
web api返回以下JSON字符串

[
{
“contentId”:“123”,
“contentJson”:{
“标题”:“ABC”,
“说明”:“ABC说明”
},
“cultureName”:“en us”
},
{
“contentId”:“124”,
“contentJson”:{
“标题”:“定义”,
“说明”:“DEF说明”
},
“cultureName”:“en us”
},
{
“contentId”:“222”,
“contentJson”:{
“标题”:“ZZZ”,
“说明”:“ZZZ说明”
},
“cultureName”:“en us”
},
{
“contentId”:“125”,
“contentJson”:{
“头衔”:“AAA”,
“说明”:“AAA说明”
},
“cultureName”:“en us”
}
]

我所需的类型在contentJson对象中。如何在Angular 2中进行转换?

使用
试试这个

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

您想将此案例用于什么目的?单靠铸造是不会有任何效果的。强制转换只是告诉您的IDE和其他开发工具,假定某个值属于特定类型是安全的。在运行时,它将被忽略-尤其是当它是您强制转换到的接口时。实际上,我只想在列表中显示contentJson对象。什么列表?你遇到了什么问题?我想得到以下数组:{“title”:“ABC”,“description”:“ABC description”},{“title”:“DEF”,“description”:“DEF description”},{“title”:“ZZZ”,“description”:“ZZZ description”},所以你只想显示这两个属性而不是所有属性?我认为您应该创建一个新的对象实例。将其强制转换到界面不会导致其他属性不显示。正如我上面提到的,接口只用于linter和编译器,但在运行时被忽略。