Ionic 2 Json解析

时间:2017-07-17 08:05:22

标签: json ionic2

我如何解析json数据

data =“{\”msg_ok \“:\”Uye olusturuldu \“,\”user_id \“:181,\”token \“:\”8650bfe987a3d619445f3d4905e1ae863e4be85f \“}”

我想使用令牌数据

我尝试过这段代码,但没有工作..

Thanx到现在

var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json' );  
//headers.append('Authorization' , 'Basic '+ btoa(tok));
let options = new RequestOptions({ headers: headers });

let postParams = {
  username: this.uyelik['username'],
  email:this.uyelik['email'],
  password:this.uyelik['password']
}

this.http.post("https://deneme.com/api/v1.0/users/", postParams, options)
  .subscribe(data => {
     console.log(data['_body']);
     this.veri = data['_body'];
    this.veri = JSON.parse(this.veri);
     console.log(this.veri['token']);
   }, error => {
    console.log(error);// Error getting the data
  });

我解决了问题;

var headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append('Content-Type', 'application/json' );  
    //headers.append('Authorization' , 'Basic '+ btoa(tok));
    let options = new RequestOptions({ headers: headers });

    let postParams = {
      username: this.uyelik['username'],
      email:this.uyelik['email'],
      password:this.uyelik['password']
    }

    this.http.post("https://iothook.com/api/v1.0/users/", postParams, options)
      .subscribe(data => {
         //console.log(data['_body']);
        veri = data['_body'];

        veri= veri.slice(1, -1);
        veri = veri.replace(/\\/g, "");
        veri = JSON.parse(veri);
        console.log(veri.token);


       }, error => {
        console.log(error);// Error getting the data
      });

2 个答案:

答案 0 :(得分:1)

试试这个。

this.http.post("https://deneme.com/api/v1.0/users/", postParams, options)
        .map((res: Response) => res.json())
          .subscribe(data => {
             console.log(data['_body']);
             this.veri = data['_body'];
            this.veri = JSON.parse(this.veri);
             console.log(this.veri['token']);
           }, error => {
            console.log(error);// Error getting the data
          });

答案 1 :(得分:0)

像这样解析 -

var a = '{\"msg_ok\": \"Uye olusturuldu\", \"user_id\": 181, \"token\": \"8650bfe987a3d619445f3d4905e1ae863e4be85f\"}';
a.replace(/\//g, "");
var token = JSON.parse(a).token;
console.log(token)