【问题标题】:How do you show JSON array in ionic using *ng-For when the keys are integer strings?当键是整数字符串时,如何使用 *ng-For 在 ionic 中显示 JSON 数组?
【发布时间】:2019-09-09 03:02:45
【问题描述】:

我正在使用 ionic 4,我正在尝试通过 http.get 使用 TransportAPI 获取 JSON 数组,但是,它们使用整数字符串作为我要获取的对象的键,然后有多个每个对象的数组,如下所示:

{
    "departures": {
        "25": [
            {
                "mode": "bus",
                "line": "25",
                "departure_time": "12:40"
            },
            {
                "mode": "bus",
                "line": "25",
                "departure_time": "13:00",
            }
        ],
        "50": [
            {
                "mode": "bus",
                "line": "50",
                "departure_time": "12:46",
            },
            {
                "mode": "bus",
                "line": "50",
                "departure_time": null,
            },
            {
                "mode": "bus",
                "line": "50",
                "departure_time": "14:46",
            }
        ]
    }
}

这个 JSON 数组存储在“testArray: any;”中并且实际获取它没有问题,因为我可以将它打印到控制台日志中。几个小时后,我才发现你必须将数字键放入括号符号,即。 ["25"] 来访问这些,但是我不确定在使用 *ngFor 时如何处理这个问题,或者即使你愿意。这是我试图输出的粗略代码:

<div *ngFor="let bus of testArray.departures"> //this is where I'm not too sure

    <ion-item-divider>
        <ion-label> bus line: {{ bus.line }}</ion-label>
    </ion-item-divider>

    <ion-item *ngFor="let time of bus"> //no idea what I'm doing here either
        {{ time.departure_time }}
    </ion-item>
</div>

任何帮助将不胜感激!

编辑:这是我用来获取 JSON 文件的代码(缺少导入和组件等以节省空间:

export class BusesPage implements OnInit {
  testArray: any;

  constructor(private http: HttpClient) {}

  fillTestArray(){
      this.http.get('assets/test.JSON').subscribe(data => {
          this.testArray = data;
          console.log(this.testArray);
      });
  }

  ngOnInit() {
      this.fillTestArray();
  }

}

【问题讨论】:

    标签: html json angular ionic4 ngfor


    【解决方案1】:

    如果这是您希望遍历的数据,您需要使用键值管道,因为这是一个对象。 *ngFor without 用于遍历数组。这将允许您遍历对象。

    然后您可以在没有键值管道的情况下遍历嵌套在对象内的数组。这应该显示你想要的数据。

    <div *ngIf="testArray">  // checks testArray exists
        <div *ngFor="let bus of testArray.departures | keyvalue">
            <div *ngFor="let data of bus.value">
                <ion-item-divider>
                    <ion-label> bus line: {{ data.mode }}</ion-label>
                    <ion-label> bus line: {{ data.line }}</ion-label>
                    <ion-label> bus line: {{ data.departure_time }}</ion-label>
                </ion-item-divider>
            </div>
        </div>
    </div>
    

    keyvalue pipe docs.

    Angular's displaying data guide.

    【讨论】:

    • 我觉得我到了某个地方,但我不断收到“无法读取未定义的属性 'departures'”,我想我会尝试通过添加“testArray?.departures”使其类型安全但那只是给了我“找不到一个不同的支持对象 '[object Object]' 类型为 'object'。NgFor 仅支持绑定到 Iterables,例如数组。”
    • 我在上面做了一个编辑,我猜数据来自 api 等,加载时有一个小等待,所以在它尝试绘制 *ngFor 之前将其包装在 *ngIf 中检查数据是否存在。如果这没有帮助,请说明您是如何获取数据的:-)
    • 我刚刚尝试添加 *ngIf 但我仍然收到“无法读取未定义的属性 'departures'”,我不再直接从 API 调用它,因为我开始用完点击(免费版本每天最多 1000 个,这非常公平)所以我将它复制到一个 JSON 文件中,我用来访问它的代码是:[添加为上面的编辑以便可以正确查看]
    • 哎呀,再次检查编辑。我认为只需要 testArray 即可。 console.log 是否显示正确的数据? testArray.departures 会出错,因为它试图访问未定义的出发,我的错。只是 *ngIf="testArray"
    • 该死,是的,console.log 正确显示了所有内容,我将“testArray.departures”更改为“testArray”,但仍然得到“找不到不同的支持对象 '[object Object]' 类型为 'object '。NgFor 只支持绑定到数组等Iterables。"
    猜你喜欢
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2013-03-04
    相关资源
    最近更新 更多