Jordan Baron
Jordan Baron

Reputation: 165

How can I get certain data from JSON using a JQuery ajax call?

I am making a small finance website as a project but I can't seem to figure out how to get specific data from the JSON that is on this Google Finance site. The code should get only the value associated with l but it gets all of the data instead.

$(document).ready(function(){
  $.ajax({
    url: 'http://finance.google.com/finance/info?client=ig&q=AMD',
    dataType: 'jsonp',
    data: { get_param: 'l' },
    success: function(json) {
        console.log(json);
    }

  });
});

Upvotes: 0

Views: 1857

Answers (2)

mtizziani
mtizziani

Reputation: 1016

$(document).ready(function(){
  $.ajax({
    url: 'http://finance.google.com/finance/info?client=ig&q=AMD',
    dataType: 'jsonp',
    success: function(json) {
        console.log(json[0]['l']);
    }
  });
});

you don't need to send a data construct on this query because google did not handle it. all you need to do work with the complete json result in your success function and figure out what params you need to attach. this sample should do what you want if i understood your question right.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337580

To achieve this you need to access the l property of the returned object. As they are in an array you need to either loop over them or get a specific one by index. Try this:

$.ajax({
  url: 'http://finance.google.com/finance/info?client=ig&q=AMD',
  dataType: 'jsonp',
  success: function(json) {
    console.log(json[0].l); // get the property from the first object
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions