0

I am receiving the below JSON which i am trying to map to the objects i have defined

{
   "nodes":[
      {
         "branchId":1,
         "branchLevel":1,
         "branchOrder":1,
         "branchDescription":"BR01",
         "leaves":[
            {
               "clTechForm":"TF01",
               "branchId":1,
               "leafOrder":1
            },
            {
               "clTechForm":"TF02",
               "branchId":1,
               "leafOrder":2
            }
         ]
      }          
   ]
}

I have the following TypeScript Classes to which i am trying to map the JSON i have received above:

export class TreeMapper{
  constructor(public nodes: BranchMapper[]) {
  }
}

export class BranchMapper{
  constructor(public id: number, public name: string, public children: TechnicalFormMapper[]  ) {
  }
}

export class TechnicalFormMapper{
  constructor(public id: number, public name: string) {
  }
} 

I would like to map the received JSON to the following JSON.

{
   "nodes":[
      {
         "id":1,
         "name":"BR01",
         "children":[
            {
               "name":"TF01",
               "id":1,
            },
            {
               "name":"TF02",
               "id":1,
            }
         ]
      }          
   ]
}

Here is my HTTP call to the Backend:

  getTrees(): Observable<TreeMapper[]> { 
    return this.http.get('some url').map(
      (response: Response) => (response.json())
    ).catch(
      (error: Response) => {
        return Observable.throw('Nodes Fetch Failed');
      }
    );
  }

Please help me out in mapping to the Object i have defined above. Any help would be appreciated.

6
  • try out let me know its worked for you or let me know if you need more help Commented Mar 23, 2018 at 11:10
  • @PranayRana No, it doesn't work the JSON received doesn't map to the TreeMapper Object even if we specify the return type as TreeMapper or the attribute type as TreeMapper Commented Mar 23, 2018 at 11:17
  • try this json2ts.com , create your class similar to your json structure , if your structure is not mapping it will not map with your object Commented Mar 23, 2018 at 12:31
  • Please start accepted answers that worked for you ...there is 0 selected answers Commented Mar 23, 2018 at 14:03
  • Sorry but it didn't worked for me! i am having the same issue.. Commented Mar 23, 2018 at 15:56

1 Answer 1

1

you dont require map, just do this

in service.ts

getTrees(): Observable<TreeMapper[]> { 
    return this.http.get('some url');
  }

in component.ts

treemappers: TreeMapper[];

getTrees() {
  this.service.getTress().subscribe(data => this.treemappers = data);
//write catch code
}

Async Pipe

if you are not doing any processing on data received from http call make use of async pipe it will do work for you

in service.ts

getTrees(): Observable<TreeMapper[]> { 
    return this.http.get('some url');
  }

in component.ts

treemappers: Observable<TreeMapper[]>;

getTrees() {
  this.service.getTress();
//write catch code
}

in component.thml

<table>
 //rest of code
 <tr *ngFor="let mapper of treemappers | async">
 //rest of code
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.