0

我無法在Angular2中的TypeScript中映射嵌套的json。 。如何在Angular 2中的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->僅從'templateId'i正在獲取數據,但不是從'sectionList.sectionName'

+0

也許是上限? SectionList - > sectionList –

+1

究竟是什麼問題?你從哪裏得到數據? – Alex

+0

我在調用this.itemService.getTemplate1()這個函數。subscribe( temp1 => this.sect1 = temp1, error => this.errorMessage = error); 但我只在Template1中獲得價值,而不是在SectionsList,SubSectionsList和FieldProperties的變量中 –

回答

0

SectionList是對象的數組。因此,您必須獲取第一個對象的sectionName,例如:

建議:最好使用訂閱。

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); 
} 
+0

是的,我打電話這個函數就像this.itemService.getTemplate1()。subscribe( temp1 => this.sect1 = temp1, error => this.errorMessage = 錯誤); 但我只在Template1中獲得價值,而不是在SectionsList,SubSectionsList和FieldProperties中 –