Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从typescript中的json对象恢复数据_Json_Typescript_Angular2 Services - Fatal编程技术网

如何从typescript中的json对象恢复数据

如何从typescript中的json对象恢复数据,json,typescript,angular2-services,Json,Typescript,Angular2 Services,我试图使用从API获取的值,该API返回JSON格式的值 我可以在控制台中看到对象的值。但是我不能分开使用它们 如何在不同的变量中分别赋值 我想在将数据发送到HTML之前验证数据 IMG 或 getData(){ 设e=md5(this.userString); 这是http .get(this.url+e) .map((res)=>res.json()) .订阅( user=>this.user=user, error=>this.errorMessage=)} 谢谢很抱歉,我无法添加注释

我试图使用从API获取的值,该API返回JSON格式的值

我可以在控制台中看到对象的值。但是我不能分开使用它们

如何在不同的变量中分别赋值


我想在将数据发送到HTML之前验证数据

IMG

getData(){
设e=md5(this.userString);
这是http
.get(this.url+e)
.map((res)=>res.json())
.订阅(
user=>this.user=user,
error=>this.errorMessage=)}

谢谢

很抱歉,我无法添加注释,但我认为它位于html页面中,在某个div中调用函数,然后使用所需的指定信息调用数据,例如,在您的情况下,它是这样的:


{{i.username}
{{i.id}

如果数据是数组,它将工作。

这就是我如何理解你的问题。

谢谢,但如果我尝试在typescript中使用相同的数据。我需要做什么?我想在将数据发送到HTML之前验证数据。很抱歉,如果我不能很好地解释它。我理解您的意思,您可以尝试这样做:全局声明一个变量,然后在函数
getData
中使用它,方法是将数据的值赋给该变量,但您必须在同一个函数中进行比较,因为在函数外部它没有定义@OsmarFerreiraNice,谢谢@Zainab Hammami。抱歉,最后一个问题是,我可以用全局变量获取
getData
函数的值ID吗?在另一个函数中使用?没问题,试试看,但我不相信它会起作用,因为它在一个函数中@奥斯马费雷拉
this.http
  .get(this.url + e)
  .map((res) => res.json())
  .subscribe((data) => this.user = data);
getData() {
let e = md5(this.userString);
this.http
  .get(this.url + e)
  .map((res) => res.json())
  .subscribe(
  user => this.user = user,
  error => this.errorMessage = <any>)}