Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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
获取数据并将json对象放入变量_Json_Angular_Angular Httpclient - Fatal编程技术网

获取数据并将json对象放入变量

获取数据并将json对象放入变量,json,angular,angular-httpclient,Json,Angular,Angular Httpclient,我想在officeidid、code、name、shortname、accroym中获取嵌套数据。并将其放入单个变量中 我该怎么做 我的代码: { "id": 1, "code": "1000-001-1-01-001-001", "name": "PEACE AND ORDER PROGRAM", "isActive": true, "majorFinalOutput": null, "officeId": 1, "office": { "id": 1, "

我想在
officeid
id、code、name、shortname、accroym中获取嵌套数据。并将其放入单个变量中

我该怎么做

我的代码:

{
  "id": 1,
  "code": "1000-001-1-01-001-001",
  "name": "PEACE AND ORDER PROGRAM",
  "isActive": true,
  "majorFinalOutput": null,

  "officeId": 1,

"office": {
  "id": 1,
  "code": "1-01-001",
  "name": "Office of the Governor",
  "shortName": "PGO",
  "accronym": "PGO",
  "website": null,
  "email": null,
  "telephone": null,
  "fax": null,
  "type": "1"
},

 "sectorId": 1,

"sector": {
  "id": 1,
  "name": "General Public Services Sector",
  "code": "1000",
  "parentId": null,
  "parent": null
},

  "dateCreated": "2018-10-02T14:23:04.913",
  "dateModified": null,
  "createdBy": null,
  "modifiedBy": null
}

 getProgram() {
     return this.httpClient.get('api/programs/' + idhold).subscribe((holdprogram: any[]) => {
    console.log(holdprogram);
    });

  return this.programService.editProgram().finally( () => {

  }).subscribe((holdprogram: any[]) => {
    console.log(holdprogram);
    console.log(holdprogram.office.id);
    console.log(holdprogram.office.name);
    console.log(holdprogram.office.shortname);
  }, error => {
    console.error(error);
  },
  () => {
  });
}

保持对通过请求获得的变量的引用的最简单方法是使用组件变量:

在组件中:

public export class MyComponent {
    ...
    public office: any; // instead of using 'any', you could create an interface corresponding to the structure

    ...
}
在订阅中:

.subscribe((holdprogram: any[]) => {
    this.office = holdprogram.office;
    console.log(this.office);
    // now this.office keeps a reference of your nested variable 'office'.
}, 

如果您需要在组件之间为它保留一个引用,那么它就有点复杂了:您可以在服务级别执行类似的操作(使用
tap
和一个局部变量),并且您需要添加更多的“缓存处理”机制。

您能否编辑您的问题并将
programService
service的代码添加到您的问题中?您想要一个单独的对象吗?将这些值放在单独的变量中可以实现什么?看起来你想解决一个问题,你想写一些垃圾代码。