我无法在Angular2中的TypeScript中映射嵌套的json。
我的Json结构是这样的:
{
"templateId": 5,
"sectionsList": [
{
"sectionName": "ITEMHEADER",
"subSectionsList": [
{
}
],
"fieldProperties": [
{
"fieldName": "CustomerItemReferenceNo",
"isUsedForTotals": "N"
},
{
"fieldName": "LFItemReferenceNo",
"isUsedForTotals": "N"
},
{
"fieldName": "ItemName",
"isUsedForTotals": "N"
},
{
"fieldName": "ItemDescription",
"isUsedForTotals": "N"
},
{
"fieldName": "LFDivision",
"value": "CMN_V_ORGANIZATION.DIVISION_CODE",
"isUsedForTotals": "N"
},
{
"fieldName": "LFDepartment",
"value": "CMN_V_ORGANIZATION.DEPARTMENT_CODE",
"isUsedForTotals": "N"
},
{
"fieldName": "LFSourcingOffice",
"value": "CMN_V_OFFICE.OFFICE_CODE",
"isUsedForTotals": "N"
}
],
"total": 0
},
{
"sectionName": "MATERIAL",
"subSectionsList": [
{
"subSectionName": "FABRIC",
"fieldProperties": [
{
"fieldName": "MaterialPriority",
"isUsedForTotals": "N"
},
{
"fieldName": "SupplierMaterialID",
"isUsedForTotals": "N"
},
{
"fieldName": "CountryofOrigin",
"value": "CMN_V_COUNTRY.COUNTRY_CODE",
"isUsedForTotals": "N"
},
{
"fieldName": "MATERIALPRICE",
"isUsedForTotals": "Y"
},
{
"fieldName": "TotalFabricCost",
"isUsedForTotals": "Y"
}
],
"totals": 0
}
],
"fieldProperties": [
],
"total": 0
},
{
"sectionName": "MATERIAL",
"subSectionsList": [
{
"subSectionName": "TRIMS",
"fieldProperties": [
{
"fieldName": "MaterialPriority",
"isUsedForTotals": "N"
},
{
"fieldName": "SupplierMaterialID",
"isUsedForTotals": "N"
},
{
"fieldName": "MaterialContent&Description",
"isUsedForTotals": "N"
},
{
"fieldName": "CountryofOrigin",
"value": "CMN_V_COUNTRY.COUNTRY_CODE",
"isUsedForTotals": "N"
},
{
"fieldName": "MATERIALPRICE",
"isUsedForTotals": "Y"
},
{
"fieldName": "TotalTrimCost",
"isUsedForTotals": "Y"
}
],
"totals": 0
}
],
"fieldProperties": [
],
"total": 0
},
{
"sectionName": "PACKAGING",
"subSectionsList": [
{
}
],
"fieldProperties": [
{
"fieldName": "Packagingcostperpackingcomponent",
"isUsedForTotals": "Y"
},
{
"fieldName": "TotalPackagingCost",
"isUsedForTotals": "Y"
}
],
"total": 0
}
]
}
而且这个类已经写好了映射Json是这样的:
export interface Template1 {
templateId: number;
sectionsList:SectionsList[];
}
export interface SectionsList {
sectionName: string;
subSectionsList:SubSectionsList[];
fieldProperties:FieldProperties[];
total:number;
}
export interface SubSectionsList {
subSectionName: string;
fieldProperties:FieldProperties[];
total:number;
}
export interface FieldProperties {
fieldName: string;
value:string;
isUsedForTotals:string;
}
我从Json映射的服务是:
getTemplate1():Observable<Template1 []>{
return this.http.get("http://localhost:8080/RestEasyWebApp/rest/restAPI/getCostSheet/1")
.map((response: Response) => response.json())
.do(data => console.log([data]))
.catch(this.handleError);
}
注意:eg-&gt;仅限于&#39; templateId &#39;我正在获取数据,但不是来自&section; sectionList.sectionName&#39;
答案 0 :(得分:0)
SectionList是对象的数组。所以你必须获取第一个对象的sectionName,如:
建议:最好使用subscribe。
getTemplate1():Observable<Template1 []>{
return this.http.get("http://localhost:8080/RestEasyWebApp/rest/restAPI/getCostSheet/1")
.map((response: Response) => response.json())
.do((data) =>
{
console.log(data);
console.log(data.sectionList[0].sectionName)
})
.catch(this.handleError);
}