繁体   English   中英

如何在Angular 2的TypeScript中映射嵌套的Json

[英]How to map the nested Json in TypeScript in Angular 2

我无法在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->仅从“ templateId ”获取数据,而不从“ sectionList.sectionName”获取数据

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

暂无
暂无

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

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