2017-07-17 6 views
0

iはJSONデータを解析することができる方法イオン2 JSONは

データ= "{\" msg_ok \を解析します8650bfe987a3d619445f3d4905e1ae863e4be85fの\ "}" 私はトークンのデータを使用したい

私はこのコードのように試みたが、動作していない...

ありがとうとなりまし

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

ドゥマッピングなどの解析。このようなもの 戻り値this.http.post(this.baseUrl + endUrl、param、options) .map((res:Response)=> res.json()); –

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

this-

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

+0

okeyは動作しますが、どのようにtoke番号のみを使用できますか? –

+0

回答が更新されました。 –

+0

戻り値:未定義 –