我正在使用angular2 typeScript從webservice.I獲取json返回通過調用get可以從WebService獲取值,但我無法獲取JSON組件。例如,我想要獲取返回的JSON的狀態。我可以得到整個JSON,但不是JSON的確切狀態。 您可以看到下面webservice返回的示例代碼和JSON。如何獲取JSON字符串的組件通過webservice請求返回
問候 的Alper
示例代碼:
this.http.get(this.webServiceUrlGet)
.subscribe(
function (data) {
this.tradeShows = JSON.stringify(data);
console.log(this.tradeShows);
},
error => this.errorMessage = <any>error);
JSON :
"_body":"{\n \"args\": {}, \n \"headers\": {\n \"Accept\": \"application/json, text/plain, */*\", \n \"Accept-Encoding\": \"gzip, deflate, sdch, br\", \n \"Accept-Language\": \"tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4\", \n \"Connect-Time\": \"0\", \n \"Connection\": \"close\", \n \"Host\": \"httpbin.org\", \n \"Origin\": \"http://localhost:3000\", \n \"Referer\": \"http://localhost:3000/navigation\", \n \"Total-Route-Time\": \"0\", \n \"User-Agent\": \"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36\", \n \"Via\": \"1.1 vegur\", \n \"X-Request-Id\": \"db6c93bd-99dd-4410-a709-5bc2dd4150fb\"\n }, \n \"origin\": \"212.175.18.38\", \n \"url\": \"https://httpbin.org/get\"\n}\n","status":200,"ok":true,"statusText":"OK","headers":{"Content-Type":["application/json"]},"type":2,"url":"https://httpbin.org/get"}
你爲什麼不使用'的console.log(this.tradeShows.status);' –
我之前嘗試過,但它告訴我undefined sachilla。 – user2307786
你需要'JSON.parse(response)'.i.e。 '數據'在你的情況下 –