Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
Arrays Angular2服务没有';t从json中检索嵌套数组_Arrays_Json_Angular - Fatal编程技术网

Arrays Angular2服务没有';t从json中检索嵌套数组

Arrays Angular2服务没有';t从json中检索嵌套数组,arrays,json,angular,Arrays,Json,Angular,我花了很多时间来解决这个问题 我有webapi返回json,比如: { "id": 1, "name": "testName", "address": [ { "id": 6, "address_name": 'testname' }, { "id": 6, "address_name": 'testname' } ] } 在angular2,我有这样的服务: public getRoutes():

我花了很多时间来解决这个问题

我有webapi返回json,比如:

{
  "id": 1,
  "name": "testName",
  "address": [
    {
      "id": 6,
      "address_name": 'testname'
    },
    {
      "id": 6,
      "address_name": 'testname'
    }
  ]
}
在angular2,我有这样的服务:

public getRoutes():Observable<Route[]>
{
      return this.http.get(this.BASE_API_URL+'/Routes')
             .map(res=>res.json());
}
export interface Route{
   id:number;
   name:string;
   address: Address[];
}
export interface Address{
   id: number;
   address_name: string;
}
this.routeService.getRoutes().subscribe(
       routes => {
           for(let route of routes.address){
                mainInstance.routes.push(route);
           }
           console.log("Routes filled");
       }
);
json结果包含id或名称等填充的简单属性,但内部数组地址为空。你们知道我在干什么吗?
感谢您的建议

在我看来,您返回的对象带有嵌套数组,但您试图将父对象本身视为数组

如果您的服务返回

{
  "id": 1,
  "name": "testName",
  "address": [
    {
      "id": 6,
      "address_name": 'testname'
    },
    {
      "id": 6,
      "address_name": 'testname'
    }
  ]
}
然后更新服务功能的签名:

public getRoutes():Observable<Route>
{
      return this.http.get(this.BASE_API_URL+'/Routes')
             .map(res=>res.json());
}
如果要在组件中使用
地址
数组,请按如下方式访问该属性:

public getRoutes():Observable<Route[]>
{
      return this.http.get(this.BASE_API_URL+'/Routes')
             .map(res=>res.json());
}
export interface Route{
   id:number;
   name:string;
   address: Address[];
}
export interface Address{
   id: number;
   address_name: string;
}
this.routeService.getRoutes().subscribe(
       routes => {
           for(let route of routes.address){
                mainInstance.routes.push(route);
           }
           console.log("Routes filled");
       }
);

通过这种方式,您访问的是
地址
字段,即数组,而不是父对象。

在我看来,您返回的对象是嵌套数组,但您试图将父对象本身视为数组

如果您的服务返回

{
  "id": 1,
  "name": "testName",
  "address": [
    {
      "id": 6,
      "address_name": 'testname'
    },
    {
      "id": 6,
      "address_name": 'testname'
    }
  ]
}
然后更新服务功能的签名:

public getRoutes():Observable<Route>
{
      return this.http.get(this.BASE_API_URL+'/Routes')
             .map(res=>res.json());
}
如果要在组件中使用
地址
数组,请按如下方式访问该属性:

public getRoutes():Observable<Route[]>
{
      return this.http.get(this.BASE_API_URL+'/Routes')
             .map(res=>res.json());
}
export interface Route{
   id:number;
   name:string;
   address: Address[];
}
export interface Address{
   id: number;
   address_name: string;
}
this.routeService.getRoutes().subscribe(
       routes => {
           for(let route of routes.address){
                mainInstance.routes.push(route);
           }
           console.log("Routes filled");
       }
);

通过这种方式,您可以访问
地址
字段,即数组,而不是父对象。

routes.address是数组(0)。所以我无法读取该属性。但当我打开webapi页面时,我看到了正确的结果。这真是奇怪的行为。我试图将一些设置添加到byt服务地图中,但没有成功。我只是编辑了答案,以展示如何构建您的模型。您在服务函数上的签名似乎与实际返回的签名不匹配,这会使TypeScript compiler.routes.address成为数组(0)。所以我无法读取该属性。但当我打开webapi页面时,我看到了正确的结果。这真是奇怪的行为。我试图将一些设置添加到byt服务地图中,但没有成功。我只是编辑了答案,以展示如何构建您的模型。看起来您在服务函数上的签名与实际返回的内容不匹配,这会使TypeScript编译器失效。