我正在尝试在 html 页面上显示 json 数据。服务器上的数据向我显示 json 数据,但是当我尝试在页面上显示它时,它给了我这个数据
{"_isScalar":false,"source":{"_isScalar":false,"source":{"_isScalar":false,"source":{"_isScalar":true,"value":{"url":"https://feeds.citibikenyc.com/stations/stations.json https://feeds.citibikenyc.com/stations/stations.json","body":null,"reportProgress":false,"withCredentials":false,"responseType":"json","method":"GET","headers":{"normalizedNames":{},"lazyUpdate ":null,"headers":{}},"params":{"updates":null,"cloneFrom":null,"encoder":{},"map":null},"urlWithParams":"https://feeds.citibikenyc.com/stations/stations.json https://feeds.citibikenyc.com/stations/stations.json"},"scheduler":null},"operator":{"concurrent":1}},"operator":{}},"operator":{}}
我获取数据的代码是
import { HttpClient} from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class GetdataService {
posts : any;
readonly ROOT_URL ="https://feeds.citibikenyc.com/stations/stations.json";
constructor(private http: HttpClient ) { }
getPosts()
{
this.posts = this.http.get(this.ROOT_URL );
return JSON.stringify(this.posts);
}
}
this.http.get()
不返回数据,而是返回一个可观察的数据,您必须订阅该数据:
getPosts() {
this.http.get(this.ROOT_URL)
.subscribe((data) => {
//DO STUFF HERE
});
}
Readmore https://angular.io/guide/http
其次,虽然修复 observable-stuff 后不需要解码 JSON,但可以使用以下命令解码 jsonJSON.parse()
not JSON.stringify
. stringify
将对象转换为字符串(与您想要的完全相反)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)