How to catch a certain json variable

Asked

Viewed 366 times

2

<script type="text/javascript">
$(".addTrack").live('click', function() {
    $('.playerTocador').css('display', 'block');
    var mp31 = $(this).attr("data-servidor");
    var session = $(this).attr("data-session");
    //$('.terra').load("http://www.mediafire.com/api/1.5/file/get_links.php?link_type=direct_download&quick_key="+mp31+"&session_token="+session+"&response_format=json");
    jQuery.ajax({
        url: "http://www.mediafire.com/api/1.5/file/get_links.php?link_type=direct_download&quick_key=" + mp31 + "&session_token=" + session + "&response_format=json", //URL de destino
        dataType: "json", //Tipo de Retorno
        success: function(json) { //Se ocorrer tudo certo
            var msg = "direct_download: " + json.response['direct_download'] + "\n";
            alert(msg);
        }
    });
});

</script>

Ex.: I need "direct_download"

outworking:

{
    "response": {
        "action": "file\/get_links",
        "links": [{
            "quickkey": "xf4d9nndyzg79cq",
            "direct_download": "http:\/\/download1326.mediafire.com\/csl7scdkjvkg\/xf4d9nndyzg79cq\/01-+Alo+Amor+-+Calcinha+Preta.mp3"
        }],
        "direct_download_free_bandwidth": "3128",
        "result": "Success",
        "current_api_version": "1.5"
    }
}

Thank you

1 answer

2

If you are sure that this link will always be returned in the array: json.response.links[0]['direct_download']

Otherwise:

var downloadLinkObject = json.response.links.filter(function(e){ return e['direct_download']; })[0];

var link = (downloadLinkObject) ? downloadLinkObject['direct_download'] : '';
  • Gustava, it worked perfectly. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.