0

i am using a third party api. which provides a nested JSON object in return. In angular 7 i have used http get request to get data. The data looks good in console. But does not bind with the HTML.

I am using angular 7

ngOnInit(){
this._dataservice.getdata().subscribe(res => {
  this.cricdata = res;
  console.log(this.cricdata);
})

}

This is the JSON Data

3
  • Then it seems there is some issue in your template. Commented Mar 25, 2019 at 18:41
  • can you please tell us in html which key you want to display and it would be great if you provide us the JSON in text instead of image Commented Mar 25, 2019 at 18:41
  • what is a data type of cricdata Commented Mar 25, 2019 at 18:42

2 Answers 2

2

This is what you want, but first, start to read the Angular Docs

<div>
{{cricdata | json}}
</div>

<div>
{{cricdata?.22374?.id}}
</div>

<div>
{{cricdata?.22374?.timeForNextDay}}
</div>

A better way is to convert your object response data into an array and then use a for loop to render the data.

An example

export class MyComponent implements OnInit {

  bucket: any[] = [];

  constructor() {}

  ngOnInit(){

    this._dataservice.getdata().subscribe(res => {
      this.cricdata = res;
      console.log(this.cricdata);

      Object.entries(res.matches).map( res => {
        bucket.push(res[1])
      });
    });

  }

}

and then use a for loop

<div *ngFor="let item of bucket;">
  {{item?.id}}
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks George.. This works. but still i am geting an warning.. " Property 'matches' does not exist on type 'Icricdata[]' ". I have created an interface of Icricdata.
1

If the JSON data had been an Array we could have used the normal *ngFor syntax, something like:

<div *ngFor="let cric of cricdata">{{cric.id}}</div>

But it looks like your data is in the form of an object that again holds objects, so we are not dealing with Arrays here. Luckily Angular has a pipe for that called the KeyValuePipe. I believe this approach can help you along:

<div *ngFor="let cric of cricdata | keyvalue">{{cric.key}}:
  <div *ngFor="let subItem of cric.value | keyvalue">
    {{subItem.key}}: {{subItem.value}}
  </div>
</div>

Documentation for the KeyValuePipe: https://angular.io/api/common/KeyValuePipe

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.