2017-11-08 6 views
0

特定のキーの特定の値を取得しようとしています。私のJSONオブジェクトは、コンソールから直接取ったものです。JSON.parse get value

0 
: 
{mail: "[email protected]", location: "England", about: "Rockstar", phoneNr: "1233425"} 

応答を取得し、

getProfile(){ 
     const obs = new Subject<Users[]>(); 
     this.http.post('http://localhost/shareVideoAppPHP/sharevideoapp-backend/readProfile.php', "1", {headers: this.headers}) 
     .subscribe((message) => { //Function data 
      const response = JSON.parse(message.text()); 
      obs.next(response); 
      console.log("Mesage------->", response); 
     }, (error) => { 
      console.log("Error from server!", error); 
     }); 
    } 

が、私はそれらのすべては、以下の印刷与える

response.location 
response["location"] 
message["location"] 
message.location 

を試してみたと存じそれを印刷しマイ角度コード:これは最後のものを除いMesage-------> undefinedをエラーを通知します​​

答えて

0

あなたの応答配列です。あなたのログには、0というインデックスが付いています。

は、私はJSONをパースする必要がないと思います

console.log(response[0]['location']); 
console.log(response[0].location); 
+0

ありがとう、これはいくつかの変更を加えて機能しました。最終コードは でした。 'response.profile [0] .location' – Fig

0

をやってみてください。まず、値からメッセージが得られます。 JSON値がメッセージから取得したものであれば、message.locationを使用して取得できます。

関連する問題