繁体   English   中英

解析JSON响应Angular 2

[英]Parse a JSON response Angular 2

有谁知道如何将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装饰器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM