首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ionic 3访问json密钥

ionic 3访问json密钥
EN

Stack Overflow用户
提问于 2018-02-22 22:20:30
回答 2查看 36关注 0票数 0

我无法从restful风格的web服务访问json响应的键。

代码语言:javascript
复制
{"_body":"{\"values\": {\"user_id\":\"1\",\"name\":\"fred test\",\"email\":\"fred@test.test\",\"username\":\"fredtest\",\"token\":\"d5f66a06ec809d70d0c52842df8dc0011d7d1ad0f2d56f50d3123da17a2489fe\"}}","status":200,"ok":true,"statusText":"OK","headers":{"pragma":["no-cache"],"content-type":["text/html;charset=UTF-8"],"cache-control":["no-store"," no-cache"," must-revalidate"],"expires":["Thu"," 19 Nov 1981 08:52:00 GMT"]},"type":2,"url":"http://localhost/PHP-Slim-Restful/api/login"}

我想访问此函数中的'values‘:(this.responseData.values)

代码语言:javascript
复制
login(){
console.log('login'+ this.userData);
// Your app login API web service call triggers
this.authService.postData(this.userData,'login').then((result) => {
  this.responseData = result;

  console.log('userdata : '+ temp);
  if(this.responseData.values){
    console.log('response: ' +  this.responseData);
    localStorage.setItem('userData', JSON.stringify(this.responseData));
    this.navCtrl.push(TabsPage);
  }
  else{
    this.showToastWithCloseButton()
  }
}, (err) => {
  console.log('erreur : '+err);
});

}

我有一个未定义的错误!

你能帮帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2018-02-22 22:37:33

我使用Observable返回json数据,并在我的方法中使用subscribe函数,并使用response.json()来转换来自RESTful the服务的JSON响应。

我的组件方法,

代码语言:javascript
复制
import {Http, Headers, Response, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Rx';

var response = this.service.post('deleteUserDetails/'+this.selectedUserId, null);
   response.subscribe((res) => {
   var response = res.json();
});

Service Post方法,

代码语言:javascript
复制
post(url: string, data : any): Observable<any> {
   let headers = new Headers();
   headers.append('Content-Type', 'application/json');

   let options = new RequestOptions({ headers: headers});
   return this.http.post(url, data,{headers: headers});
}

我认为这可能会对您的查询有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2018-02-22 22:51:07

您可以在JSON中创建一个for,并访问post的返回值。差不多吧。

"this.responseData = result.json();“->返回JSON。做一个for。

示例:

代码语言:javascript
复制
public postData(data, url: string) {
    this.http.post(url, data).toPromise().then(res => {
        let responseData = res.json();
        if (responseData) {
            for (var item of responseData) {            
                //Implments
            }
        }
    }, (err) => {

    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48929725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档