cetinDev
cetinDev

Reputation: 319

Ionic 2 Json parse

How can i parse json data

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

I want to use token data

I tried like this code but not working..

Thanx to now

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
  });

I SOLVED PROBLEM ;

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
      });

Upvotes: 0

Views: 248

Answers (2)

Swapnil Patwa
Swapnil Patwa

Reputation: 4099

Parse like this-

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

Upvotes: 0

Missak Boyajian
Missak Boyajian

Reputation: 2245

Try This.

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
          });

Upvotes: 1

Related Questions