How to get a certain variable json

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>

Eg: I need the "direct_download"

result:

{
    "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

    
asked by anonymous 28.03.2016 / 17:59

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'] : '';
    
28.03.2016 / 18:12