Sudhin
Sudhin

Reputation: 87

How to get specific value from iTunes API using Javascript

This is my API call: https://itunes.apple.com/search?term=cut+the+cord&limit=1

I only want to get the value of the '"releaseDate":"2015-06-29' part.

Is there any way that I could only get that part in a textbox. I have this textbox in another page. Any ideas or suggestions?

Upvotes: 0

Views: 389

Answers (2)

stdob--
stdob--

Reputation: 29172

Due to the cross-domaine policy, you must use JSONP to get json data and then format response value:

$.ajax({
    url: "http://itunes.apple.com/search?term=cut+the+cord&limit=1",
    dataType: "jsonp",
    success: function( response ) {
        console.log( response );
        var d = new Date(response.results[0].releaseDate);
        var y = d.getFullYear();
        var m = pad( d.getMonth()+1, 2);
        var d = pad( d.getDate(), 2);
        $("#release").attr('value', y + "-" + m + "-" + d)
    }
});

http://jsfiddle.net/yts728L5/2/

Upvotes: 1

Christoffer Karlsson
Christoffer Karlsson

Reputation: 4943

I guess you're parsing your result as JSON:

var result = JSON.parse(apiCallResult)

Then, assuming you only have one result as in the example request above, you should be able to get that value by simply accessing the object:

var results = result.results;
var releaseDate = results[0].releaseDate;

This will work if you have multiple results also, but it will only take the first result's releaseDate.

To add the text to a text box, you can use jQuery to get the text box and then set its value:

var textBox = $("#myTextBoxId");
textBox.val(releaseDate);

Upvotes: 1

Related Questions