ni8mr
ni8mr

Reputation: 1785

Accessing a value in JSON

I am trying to access the country names from this json -

https://restcountries.eu/rest/v1/all

This is my code for loading that json -

<script>
      (function() {
           var country = "https://restcountries.eu/rest/v1/all";
           $.getJSON( country)
           .done(function( data ) {
                $.each(data, function(key, value) {
                    $('#mySelect').empty();
                    $('#myselect').append('<option>' + data.name + '</option>');
                });
             });
         })();

  </script>

The problem may be in data.name statement. I couldn't find any solution on my own. Plus, i am new to JSON. Any help or at least any comment to point my faults will be appreciated.

Upvotes: 1

Views: 71

Answers (1)

Bill Smith
Bill Smith

Reputation: 136

it should be value.name, data is the array you get from the api, value is each item in the array. also, remove empty as it's remove everything in each loop cyle...

see working code here

Upvotes: 1

Related Questions