Ali Ghassan
Ali Ghassan

Reputation: 1180

Json Array Cannot find a differ supporting object

l am try to get weather data json for wunderground api using ionic 4 . When l try to run this code l got error ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

short json response

{
  "hourly_forecast": [
    {
      "condition": "غائم جزئياً",
      "icon": "partlycloudy",
      "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
      "fctcode": "2",
      "sky": "30"
    }
  ]
}

code

weather : any 

constructor(private https: HttpClient) {


  this.https.get('xxxxxxxxxxxx/q/muscat.json')
  .subscribe(data => {

    this.weather= data
    console.log(this.weather)

  })

}

html

   <tr  *ngFor="let item of weather">

        <td>{{item.hourly_forecast.condition}}</td>
      </tr>

any idea please ?

Upvotes: 0

Views: 116

Answers (4)

Nishant Tamilselvan
Nishant Tamilselvan

Reputation: 322

Here is the sample code which works!.

var json = {
  "hourly_forecast": [{
      "condition": "غائم جزئياً",
      "icon": "partlycloudy",
      "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
      "fctcode": "2",
      "sky": "30"
    },
    {
      "condition": "غائم جزئياً",
      "icon": "partlycloudy",
      "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
      "fctcode": "2",
      "sky": "30"
    }
  ],
  "hourly_forecast2": [{
    "condition": "غائم جزئياً",
    "icon": "partlycloudy",
    "icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "fctcode": "2",
    "sky": "30"
  }]
}

// If you want iterate inside

for (var element in json) {
  var data = json[element];
  for (var val in data) {
    console.log(data[val].condition)
  }
}

Or else check whether data has been imported correctly

this.https.get('xxxxxxxxxxxx/q/muscat.json')
 .subscribe(data => {
this.weather = data.hourly_forecast;
 });

Now it will work

<tr  *ngFor="let item of weather">
    <td>{{item.hourly_forecast.condition}}</td>
  </tr>

Upvotes: 1

yer
yer

Reputation: 1532

NgFor can only be used on arrays. Try something like this:

*ngFor="let item of weather.hourly_forecast" and <td>{{item.condition}}</td>

Upvotes: 0

Roberto Zvjerković
Roberto Zvjerković

Reputation: 10157

Your data is an object, not an array. Array is wrapped in .hourly_forecast field of the object:

<tr *ngFor="let item of weather?.hourly_forecast">
    <td>{{item.condition}}</td>
</tr>

Make sure to add an ? so you don't get an error before the data arrives.

Upvotes: 2

GaryB
GaryB

Reputation: 384

 <tr  *ngFor="let item of weather.hourly_forecast.condition">
    <td>{{item}}</td>
 </tr>

This one will work

Upvotes: 3

Related Questions