typescript - 解析 JSON 响应 Angular 2

标签 typescript angular

有谁知道如何将 JSON 响应解析为这样的结构?

   [
  {
    "iD": 2,
    "OrderList": [
      {
        "bidPrice": 0,
        "iD": 0,
        "name": "Name",
        "price": 10,
        "resteurant": "Restaurant"
      },
      {
        "bidPrice": 0,
        "iD": 0,
        "name": "Something",
        "price": 20,
        "resteurant": "La Place"
      }
],
    "owner": "Tom"
  }
]

我使用 json2ts 创建了这两个结构:

  import {OrderList} from "./OrderList.ts";
import {Injectable} from  "@angular/core";

@Injectable()

export interface OrderBasket {
    iD: number;
    OrderList: OrderList[];
    owner: string;
}

import {Injectable} from  "@angular/core";

@Injectable()
export interface OrderList {
    bidPrice: number;
    iD: number;
    name: string;
    price: number;
    resteurant: string;
}

我调用服务器并订阅,但我无法获取此结构。 谁能帮忙?

最佳答案

我认为您可以尝试以下方法:

this.http.get('http://...')
  .map(res => <OrderBasket[]>res.json())
  .subscribe(data => {
    // data corresponds to a list of OrderBasket
  });

您需要了解使用 TypeScript 进行转换的含义:

否则,我不明白你为什么要在界面上使用 Injectable 装饰器。

关于typescript - 解析 JSON 响应 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062419/

相关文章:

css - 通过Angular中的插值将CSS应用于 typescript 变量的某些部分

angular - 调用函数时清除 Observable 值

javascript - 如何在表中创建效果rowspan

javascript - 如何在 TypeScript 中使用two.js

angular - 如何在 *ngFor 中设置唯一的模板引用变量? ( Angular )

angular - 带 Angular 工作箱

Angular5 调试在 Chrome 中不起作用

javascript - AngularJS 4 上的下拉值总和

javascript - 无法从 kendotreelist 中的 kendo 模板调用 typescript 中的函数

http - 从 URL 下载文件到用户可访问的位置