【问题标题】:How get values and key in the object array JSON如何在对象数组 JSON 中获取值和键
【发布时间】:2020-06-18 14:38:02
【问题描述】:

我在下面有一个 JSON:

{
  "sports": [
    { "id": 1, "name": "Soccer" },
    { "id": 2, "name": "Basketball" }
  ]
}

如何读取 typeScript 中的 id 和 name ? 目前我使用

let result = this.dataService.getJSON()

public getJSON(): Observable<any> {
  return this.http.get('./assets/json/data.json');
}

但我有一条错误消息

map is not a function

我不知道如何解析 JSON,

我想要这个结果。

[
  { "id": 1, "name": "Soccer" },
  { "id": 2, "name": "Basketball" }
]

感谢您的帮助。

【问题讨论】:

标签: javascript json typescript


【解决方案1】:

如果你使用 Observables,你需要订阅线程来接收响应:

this.dataService.getJSON().subscribe(data => {
  console.log(data);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多