ghula
ghula

Reputation: 18

How to parse api response with jquery

I need help with parsing some data. I've spend to much time on this already, so I've decided to ask you for your help.

This is my source: http://www.coincap.io/history/30day/STRAT

How can I access only market_cap values? I need to access each value, so I can later put together some string, that I have in mind.

I've done some similar thing with PHP before, but now I need to do this with jQuery or JS - and here everything stopped.

So far I've got this:

url = 'http://www.coincap.io/history/30day/BTC';

$.getJSON(url, function(data){
  $.each(data, function (index, value) {
    console.log(value);
  });
});

I think I'm on the right track, but I do not have experience with syntax.

I also tried with -> and [] approach, but I did not manage to make it work.

Upvotes: 0

Views: 1157

Answers (2)

ggsplet
ggsplet

Reputation: 706

Try something like this. I think that this should solve your problem.

    // Url to api - response
    url = 'http://www.coincap.io/history/30day/BTC';

    $.getJSON(url, function(data) {

      // Get market cap values
      var market_cap = data.market_cap;

      // Loop through
      for(var i=0; i < market_cap.length; i++)
      {
          // market_cap[i][0] - to access first value
          // market_cap[i][1] - to access second value
      }         
    });

Upvotes: 1

Arun CM
Arun CM

Reputation: 3435

You can access the market cap values like data.market_cap

url = 'http://www.coincap.io/history/30day/BTC';

$.getJSON(url, function(data) {

  //To get only market cap values.
  var market_cap = data.market_cap;

  console.log(market_cap.length)
  // 699

  //for(var i=0; i < market_cap.length;i++)
  //{
  //  loop through..
  //}

  // can access via index.
  console.log(market_cap[0])
  // [
  //  1496216056000,
  //  36388076039
  // ]


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

Related Questions