{“{u isScalar”:false,“source”:{“{u isScalar”:false,“source”:{“{u isScalar”:false,“source”:{“{u isScalar”:true,“value”:{“url”: 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 “}”,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() 不返回数据,而是要订阅的可观察数据:
this.http.get()
getPosts() { this.http.get(this.ROOT_URL) .subscribe((data) => { //DO STUFF HERE }); }
Readmore
其次,虽然在修复了可观察的内容之后不需要对JSON进行解码,但是可以使用 JSON.parse() 不 JSON.stringify stringify 将对象转换为字符串(与所需完全相反)。
JSON.parse()
JSON.stringify
stringify