简体   繁体   English

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

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

I am not able to map the nested json in TypeScript in Angular2. 我无法在Angular2的TypeScript中映射嵌套的json。 .

My Json structure is like this: 我的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
    }
  ]
}

And the class is have written is to map the Json is like this: 而该类写的就是映射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;
 }

And my service to map from Json is: 我从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);
    } 

Note: eg-> Only from ' templateId ' i am getting data but not from 'sectionList.sectionName' 注意:eg->仅从“ templateId ”获取数据,而不从“ sectionList.sectionName”获取数据

SectionList is array of object. SectionList是对象的数组。 So you have to fetch sectionName of first object like: 因此,您必须获取第一个对象的sectionName,例如:

Suggestion : better use 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);
} 

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

相关问题 How to map values from nested Typescript object to properties of JSON object - How to map values from nested Typescript object to properties of JSON object 如何将复杂的嵌套JSON从Angular Typescript发送到Web Api? - How to send complex nested JSON from Angular Typescript to Web Api? 使用HttpClient Angular将JSON映射到TypeScript类 - Map JSON to TypeScript Class using HttpClient Angular 如何使用Typescript接口映射json? - how to map json with typescript interface? angular 2如何使用Observable分配JSON响应数组并在打字稿界面中映射并使用* ngFor在html中检索? - angular 2 how to assign an array of JSON response using Observable and map in typescript interface and retrieve in html using *ngFor? 如何将嵌套JSON树的所有节点转换为angular 2 Typescript中的类的实例? - How to cast all nodes of a nested JSON tree to instances of a class in angular 2 Typescript? 如何在TypeScript中对嵌套集合进行JSON序列化 - How to JSON Serialize Nested Collections in TypeScript 如何从TypeScript中的嵌套JSON获取数据? - How to get data from nested JSON in TypeScript? 如何将 JSON 属性解析为 Typescript 中的 Map 键? - How to parse JSON property as Map key in Typescript? 我如何在angular js google map中传递嵌套的json数组对象 - how i pass nested json array object in angular js google map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM