Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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
如何在angular模板中显示json web响应?_Json_Angular_Typescript - Fatal编程技术网

如何在angular模板中显示json web响应?

如何在angular模板中显示json web响应?,json,angular,typescript,Json,Angular,Typescript,我试图显示一些JSON数据作为HTTP web响应,但它不起作用 下面是我的http请求: nodes: any; ngOnInit(): void { // Make the HTTP request: this.http.get('/assets/TEST.json').subscribe(data => { // Read the result field from the JSON response. this.nodes=data

我试图显示一些JSON数据作为HTTP web响应,但它不起作用

下面是我的http请求:

nodes: any;

ngOnInit(): void {
    // Make the HTTP request:
    this.http.get('/assets/TEST.json').subscribe(data => {
        // Read the result field from the JSON response.
        this.nodes=data;
    });
}
这是模板:

<div *ngFor="let node of nodes">
    <p>{{node.description}}</p>
</div>
<p *ngIf="nodes">{{ nodes.name }}</p>
这是我在控制台中遇到的错误:

找不到类型为“root1”的不同支持对象“[object object]”,其中root1是与发出请求的组件不同的组件


首先,JSON是一个对象,而不是数组。因此,您不能使用
ngFor
。由于对象没有
说明
属性(而是
名称
),因此可以使用此模板显示名称(确保已设置节点):

<div *ngFor="let node of nodes">
    <p>{{node.description}}</p>
</div>
<p *ngIf="nodes">{{ nodes.name }}</p>
{{nodes.name}


首先,JSON是一个对象,而不是数组。因此,您不能使用
ngFor
。由于对象没有
说明
属性(而是
名称
),因此可以使用此模板显示名称(确保已设置节点):

<div *ngFor="let node of nodes">
    <p>{{node.description}}</p>
</div>
<p *ngIf="nodes">{{ nodes.name }}</p>
{{nodes.name}


在我们对解决方案进行调查后:

  • *ngFor
    不应该在那里使用,因为他的JSON是一个对象而不是数组
因此,解决办法很简单:

<p> id : {{nodes.id}}</p>
<p> name : {{nodes.name}}</p>
<p> link : {{nodes.link}}</p>
id:{{nodes.id}

名称:{{nodes.name}

链接:{{nodes.link}


在我们对解决方案进行调查后:

  • *ngFor
    不应该在那里使用,因为他的JSON是一个对象而不是数组
因此,解决办法很简单:

<p> id : {{nodes.id}}</p>
<p> name : {{nodes.name}}</p>
<p> link : {{nodes.link}}</p>
id:{{nodes.id}

名称:{{nodes.name}

链接:{{nodes.link}


你试过
this.nodes=data.json()
?@Med\u Ali\u Rachid是的,我试过了,但没用尝试记录
数据
=>
控制台.log(data)
,它显示了json@eli@Med_Ali_Rachid是的,我将json放在控制台中json的性质是什么?它是数组还是对象?你能编辑这篇文章并显示你试过的jsondid的截图吗?@Med_Ali_Rachid是的,我试过了,但不起作用。试着记录
数据
=>
控制台。记录(数据)
,它显示了json@eli@Med_Ali_Rachid是的,我将json放在控制台中json的性质是什么?它是数组还是对象?你能编辑帖子并显示你的JSON截图吗?如果我有两个或更多的JSON对象呢?如果它有(即,你的文件包含一个数组),那么你可以使用
ngFor
。如果我有两个或更多的JSON对象呢?如果它有(即,你的文件包含一个数组),那么你可以使用
ngFor