JSON.parse 获取值

我正在尝试检索给定键的特定值。我的 JSON 对象看起来像直接从控制台获取的。

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除了最后一个给出错误Property 'location' doesn't exist on type Response

用户4676340

因为您的响应是一个数组。您可以在日志中看到该0索引。

尝试做一个

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章