有谁知道如何将 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/