简体   繁体   中英

Unable to retrieve data objects from my JSON file

I've created a JSON file to call out the name of a list of beers to display ABV and country but I am unable to display the results on the webpage. I was able to get the select tag to drop down the list, but when selecting a beer, it will only show the selected results as "undefined."

Here is the JS code I have so far...

var $select = $("#beerListing");
var beer = Array();
var country = Array();

$.getJSON("data.json", function(data) {
  $select.html('');

  for (var i = 0; i < data['beer'].length; i++)
    $select.append('<option id="' + data["beer"][i]['id'] + '">' + data["beer"][i]["beer_name"] + '</option>');

  for (x in data) {
    if (beer.indexOf(data[x].beer_name) < 0) {
      var y = beer.length;
      beer[y] = data[x].beer_name;
      country[y] = data[x].brewery_country;
    }
  }

  showBeerList();
});

function showBeerList() {
  var select = document.getElementById('beerListing');

  for (var i = 0; i < beer.length; i++) {
    var obj = document.createElement("option");
    obj.text = beer[i];
    obj.value = i;
    select.appendChild(obj);
  }
}

function getBeerInfo(picked) {
  if (picked == "Pick Your Poison...") {
    location.reload();
  } else {
    document.getElementById("name").innerHTML = beer[picked];
    document.getElementById("country").innerHTML = country[picked];
  }
}

HTML:

<html>
  <head></head>
  <body>
    <h1>LCBO API TESTING</h1>

    <select name="beerlist" id="beerListing" class="form-control" onchange="getBeerInfo(this.value)">

    </select>
    <br>
    <label>Name:</label>
    <label id="name">--</label>
    <br>
    <label>Country:</label>
    <label id="country">--</label>
    <br>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="main.js"></script>
  </body>
</html>

JSON List called data.json

{
    "beer": [{
        "beer_name": "Organic Devon Cider",
        "brewery_name": "Luscombe Organic Drinks",
        "beer_type": "Cider",
        "beer_abv": "4.9",
        "beer_ibu": "0",
        "comment": "",
        "venue_name": "The Anchor & Hope",
        "venue_city": "London",
        "venue_state": "Greater London",
        "brewery_country": "England"
    }, {
        "beer_name": "Beer A",
        "brewery_name": "Beer A",
        "beer_type": "Cider",
        "beer_abv": "4.9",
        "beer_ibu": "0",
        "comment": "",
        "venue_name": "Beer",
        "venue_city": "New York",
        "venue_state": "New York",
        "brewery_country": "USA"
    }]
}

I got it working here: https://jsfiddle.net/bu7pkb5f/1/

What are you doing with:

if (beer.indexOf(data[x].beer_name) < 0) {

    var y = beer.length;
    beer[y] = data[x].beer_name;
    country[y] = data[x].brewery_country;

}

I don't understand it but it's creating a third item in the list after the two real beer entries are processed. I left it commented out in the fiddle so you can check it out for yourself.

You seemed to be adding the options to the select element twice and using for-in which iterates properties, not entries in an array.

Below snippet will not work as requires external data source.

 var $select = $("#beerListing") ; var beer = Array(); var country = Array(); $.getJSON("data.json", function(data) { $select.html(''); for (var i = 0; i < data.beer.length; i = i + 1) { if (beer.indexOf(data.beer[i].beer_name) < 0) { beer.push(data.beer[i].beer_name); country.push(data.beer[i].brewery_country); } } showBeerList(); } function showBeerList() { var select = document.getElementById('beerListing'); for (var i = 0; i < beer.length; i++) { var obj = document.createElement("option"); obj.text = beer[i]; obj.value = i; select.appendChild(obj); } } function getBeerInfo(picked) { if (picked == "Pick Your Poison...") { location.reload(); } else { document.getElementById("name").innerHTML = beer[picked]; document.getElementById("country").innerHTML = country[picked]; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1>LCBO API TESTING</h1> <select name="beerlist" id="beerListing" class="form-control" onchange="getBeerInfo(this.value)"> </select> <br> <label>Name:</label> <label id="name">--</label> <br> <label>Country:</label> <label id="country">--</label> <br> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM