【发布时间】:2019-08-02 10:25:49
【问题描述】:
我在尝试提取嵌套 JSON 数据中的特定值时遇到了麻烦,我一直在无休止地搜索并尝试了许多不同的路线,但都无济于事。基本上,我想获取“摘要”的所有实例并将它们显示为指令(列表项),如下所示:
- 到芬奇利路的大都会线
- 银禧线到滑铁卢
我仍然是 AngularJS 的新手,因此非常感谢任何帮助,我想我可能过于复杂了。
JSON
{
"$type": "Tfl.Api.Presentation.Entities.JourneyPlanner.ItineraryResult, Tfl.Api.Presentation.Entities",
"journeys": [
{
"$type": "Tfl.Api.Presentation.Entities.JourneyPlanner.Journey, Tfl.Api.Presentation.Entities",
"startDateTime": "2019-03-11T21:22:00",
"duration": 49,
"arrivalDateTime": "2019-03-11T22:11:00",
"legs": [
{
"$type": "Tfl.Api.Presentation.Entities.JourneyPlanner.Leg, Tfl.Api.Presentation.Entities",
"duration": 34,
"instruction": {
"$type": "Tfl.Api.Presentation.Entities.Instruction, Tfl.Api.Presentation.Entities",
"summary": "Metropolitan line to Finchley Road",
"detailed": "Metropolitan line towards Aldgate",
"steps": []
},
},
{
"$type": "Tfl.Api.Presentation.Entities.JourneyPlanner.Leg, Tfl.Api.Presentation.Entities",
"duration": 13,
"instruction": {
"$type": "Tfl.Api.Presentation.Entities.Instruction, Tfl.Api.Presentation.Entities",
"summary": "Jubilee line to Waterloo",
"detailed": "Jubilee line towards Stratford",
"steps": []
},
],
}
]
HTML
<ul ng-repeat="x in data.journeys[0]">
<li ng-repeat="y in x.legs[0]">{{y.instruction.summary}}</li>
</ul>
【问题讨论】:
-
您的页面中的
li数量是否正确?因为你的外部重复在journeys[0],这对我来说似乎不正确。它应该只是 data.journeys。与 x.legs 相同。它应该只是 x.legs 而不是 x.legs[0] -
感谢@PM,我取出了 [0],它显示了数据,但也显示了 JSON 下方的“旅程”。我只需要顶级实例,将代码更改为
x in data.journeys | limitTo : 1似乎可行,但这是最好的方法吗? -
Marc,我认为这是正确的方法,如果你总是想从旅程中获得旅程的顶级实例。另一种方法可能是
<li ng-repeat="y in data.journeys[0].legs">但对我来说,您当前的方法看起来更好。
标签: html arrays angularjs json object