Home > front end >  How to use observables JSON data obtained
How to use observables JSON data obtained

Time:12-04

Now I need to get data from a json data, finally need to get the results the name attribute, and form an array,

The current url="http://swapi.dev/api/planets/? Page=1
'
I want to pass here next attribute to know whether the current page and information, because when next: null, on behalf of the data is the last page

 
{
"Next" : "http://swapi.dev/api/planets/? Page=2 ",
"Previous" : null,
"The results" : [
{
Name: planet1
}
{
Name: planet2
}
.
]
}



 
Type Planet={
Name: string;
}

Type Data={
https://bbs.csdn.net/topics/Next: string;
Results: the Planet [];
}

Url="http://swapi.dev/api/planets/? Page=1 ';

GetData (url: string=this. Url) : Observable {
Return this. HTTP get (url). The pipe (map ((res: {next: string, results: the Planet []})=& gt; {
Return {
Next: res. Next,
Results: res. Results,
}
}))
}



When using the
 

Url="http://swapi.dev/api/planets/? Page=1 ';
Planets=[];
Stop=false;

GetPlanets () {
while(! This. Stop) {
Enclosing getData (this url). The subscribe (res=& gt; {
This. Url=res. Next;
This. Planets. Push (... Res. Results);
The console. The log (' this. Planets', enclosing planets).
If (this. Url===null) {this. Stop=true};
})
}

}


I want to the console. Here is the log to push all the result of the page, but in fact I at run time what all show not to come out on display, direct collapsed,

Excuse me what method can be implemented, where is my this question?

Thank you very much!

CodePudding user response:

Ts? Probably saw, something in the code used, so regardless of your other code before

Just see you return data structure, the url should be a string? Or say no return would be: url: null? . If not, you cannot use this url===null, it will never be reached, the stop forever to false, while has been, should to overflow, will crash

If you all have no problem, you try, if (this. Url=='null') {this. Stop=true};
  • Related