简体   繁体   English

Angular中HTTP json获取数据的方法

[英]How to get data from HTTP json in Angular

kindly help I am trying to get Json data from HTTP link请帮助我试图从 HTTP 链接获取 Json 数据

<span class="subjectSpan">{{ title }}</span>

and the.ts file conatins和.ts文件conatins

  constructor(public dialog: MatDialog, private api:ServiceUserService) {};
  title:any;
  ngOnInit() {
      this.title = this.api.apiCall().subscribe(data => {
      this.title = data;
    })
  }

and the.service.ts file has而 .service.ts 文件有

apiCall() {
    return this.httpClient.get('https://jsonplaceholder.typicode.com/todos/1');
  }

I have imported HTTPCLIENTMODULE in module.ts and.ts file我在 module.ts 和 .ts 文件中导入了 HTTPCLIENTMODULE

Just the data from json is not showing just仅来自 json 的数据不显示

[object object] is coming as result. [object object]作为结果出现。

the json file contains json 文件包含

{"status":"success","results":2,"data":{"subject":[{"subject":"ART & HUMANITIES","numTitles":1,"slug":"art-humanities"},{"subject":"ANTHROPOLOGY","numTitles":1,"slug":"anthropology"}]}}

or getting this error或出现此错误

Element implicitly has an 'any' type because expression of type '"title"' can't be used to index type 'Object'.
  Property 'title' does not exist on type 'Object'.

As it was mentioned before it shows [Object object] because you are sending whole object.正如之前提到的,它显示 [Object object] 因为您发送的是整个 object。

If you want to see it works correctly change如果你想看到它正常工作改变

<span class="subjectSpan">{{ title }}</span>

to

<span class="subjectSpan">{{ title | json }}</span>

If you want to get just one of the properties of the object then I would suggest to create interface for the object.如果您只想获得 object 的其中一个属性,那么我建议为 object 创建interface

This way you can type variable title to correct object (having specified properties) and in template you can get whatever you want by for example {{title.success}}通过这种方式,您可以键入变量title以更正 object(具有指定的属性),并且在模板中您可以获得任何您想要的内容,例如{{title.success}}

Also I would suggest you to type the return value of the function. Change this另外我建议你输入 function 的返回值。改变这个

apiCall() {
return this.httpClient.get('https://jsonplaceholder.typicode.com/todos/1');

} }

to

apiCall(): Observable<**interface you have created**>{
    return this.httpClient.get<**interface you have created**>('https://jsonplaceholder.typicode.com/todos/1');
  }

EDIT: It would be nice to assign data to variable only when they exist so in this case add condition in your subscription.编辑:最好仅在数据存在时将数据分配给变量,因此在这种情况下,请在您的订阅中添加条件。

It is showing [Object object] in title because you are passing the whole json object to the title variable declared in the component, whereas you should be specifying the key in the received json Object. For example: In your json response you have "subject":"ART & HUMANITIES" json -> data -> subject -> 0th index -> subject它在标题中显示[对象对象],因为您将整个 json object 传递给组件中声明的标题变量,而您应该在收到的 json Object 中指定键。例如:在您的 json 响应中,您有“主题":"ART & HUMANITIES" json -> 数据 -> 主题 -> 第 0 个索引 -> 主题

Therefore you might want to change the line this.title = data;因此,您可能想要更改行 this.title = data; to this.title = data['data']['subject'][0]['subject']; to this.title = data['data']['subject'][0]['subject'];

also this.api.apiCall().subscribe(...) wont return title so remove that assignment. this.api.apiCall().subscribe(...) 也不会返回标题,因此删除该分配。

{{ todos? {{待办事项? todos.id: "" }} todos.id: "" }}

todos exist data then display data. todos 存在数据然后显示数据。

Typescript public todos: any; Typescript公共待办事项:任何;

value assign public varchar值分配 public varchar

todos = res;待办事项 = res;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM