0

I am trying to get the information from an api with JSON format, with jQuery. I can get or display the first index "meta" but not the second "results" (check the url)

{  
   "meta":{  
      "name":"openaq-api",
      "license":"CC BY 4.0",
      "website":"https://docs.openaq.org/",
      "page":1,
      "limit":100,
      "found":160
   },
   "results":[  
      {  
         "location":"Aberdeen",
         "city":"Aberdeen",
         "country":"GB",
         "count":23206,

This is what I can display in the html:

openaq-api
CC BY 4.0
https://docs.openaq.org/
1
100
160
[object Object]
[object Object]
[object Object]
[object Object]

I want to get in jQuery because later I want to display with this data a line chart, like Morris.js https://morrisjs.github.io/morris.js/index.html#getting-started

This is de code:

$.getJSON( "https://api.openaq.org/v1/locations?country=GB", function( data ) {
  var html = [];
  $.each( data, function( index, hees ) {
      $.each( hees, function( att, val ) {
        html.push( "<li id='" + att + "'>" + val + "</li>" );
      });
  });

  $( "<ul/>", {
    "class": "my-new-list",
    html: html.join( "" )
  }).appendTo( "#locations" );
});

Any idea?

Adrian Vazquez
  • 327
  • 1
  • 6
  • 20
  • Results is an array, you are going to have to loop over it's contents. However if each element has the same keys, then your markup generation will need to change as ids cannot repeat themselves in markup. Can you provide a more complete example of the JSON? If you want to try tackling it yourself, there is a jQuery convenience method `$.isArray(...)` that you can use. You could use that to check what 'hees' is. If it is an array, do another sub loop. – Taplar Nov 19 '16 at 18:19
  • Hi, thanks I think I know what you mean. And you just have to click on "https://api.openaq.org/v1/locations?country=GB" – Adrian Vazquez Nov 19 '16 at 18:25
  • When in doubt log the arguments of `each` to console so you can see what you are working with at each step – charlietfl Nov 19 '16 at 18:29

0 Answers0