如何使用angular2访问数组(JSON)数组

时间:2017-03-26 12:35:03

标签: arrays json angular

我有这个JSON响应

places":[{"libPlace":"Le Colis\u00e9e","idPlace":1},[{"dateEventPlace":"2017-03-19T08:09:00+0000"},{"dateEventPlace":"2017-03-19T14:19:00+0000"},{"dateEventPlace":"2017-03-24T14:08:00+0000"}],{"libPlace":"ABC","idPlace":2},[{"dateEventPlace":"2017-03-22T14:10:00+0000"},{"dateEventPlace":"2017-03-24T16:20:00+0000"}]]

我想得到类似的东西(我正在使用Angular2)

libPlace : 2017-03-19T08:09:00+0000
           2017-03-19T08:09:00+0000
           2017-03-19T08:09:00+0000

我试过这个,它只返回“libPlace”值

          <div *ngFor="let place of places " class="day clearfix">
            {{place.libPlace}}
                <div *ngFor="let times of place ">
                   {{times?.dateEventPlace}}
                </div>
            </div>

这是我的component.ts

places: any[];

ngOnInit(){
    this.route.params

        this.route.params
        .switchMap((params: Params) =>{

            return this.movieService.getPlaces(+params['id']);
    })  
        .subscribe((places: any) => {this.places = places;


        });

我也尝试将此发送到angular2并使用管道或_groupBy(来自_Underscore.js)忽略重复项,但仍然无法与我一起使用

"places":[{"libPlace":"Le Colis\u00e9e","idPlace":1,"dateEventPlace":"2017-03-19T08:09:00+0000"},{"libPlace":"Le Colis\u00e9e","idPlace":1,"dateEventPlace":"2017-03-19T14:19:00+0000"},{"libPlace":"Le Colis\u00e9e","idPlace":1,"dateEventPlace":"2017-03-24T14:08:00+0000"},{"libPlace":"ABC","idPlace":2,"dateEventPlace":"2017-03-15T07:13:00+0000"},{"libPlace":"ABC","idPlace":2,"dateEventPlace":"2017-03-22T14:10:00+0000"},{"libPlace":"ABC","idPlace":2,"dateEventPlace":"2017-03-24T16:20:00+0000"}]}

1 个答案:

答案 0 :(得分:1)

要做你想做的事,你的json是完全错的,它应该是:

places":[
{
   "libPlace":"Le Colis\u00e9e",
   "idPlace":1,
   "times":[
      {"dateEventPlace":"2017-03-19T08:09:00+0000"},
      {"dateEventPlace":"2017-03-19T14:19:00+0000"},
      {"dateEventPlace":"2017-03-24T14:08:00+0000"}
   ]
},
{
   "libPlace":"ABC",
   "idPlace":2,
   "times":[
       {"dateEventPlace":"2017-03-22T14:10:00+0000"},
       {"dateEventPlace":"2017-03-24T16:20:00+0000"}
   ]
}
]

第二个for循环应该是* ngFor =&#34;让时间放置时间&#34;。

在你的json中,时间与libplace不在同一个对象中,所以你的libplace和时间之间没有匹配。