javascriptjqueryajaxparsingjsonp

How can I get value from jSON using javascript?


How can I parse "h" value from the following jSON using javascript?

JSON Content:

info = { "title" : "Laura Branigan - Self Control", "image" : "http://i.ytimg.com/vi/p8-pP4VboBk/default.jpg", "length" : "5", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "http://ping.aclst.com/ping.php/2452159/p8-pP4VboBk?h=761944", "h" : "83135b0b3cf927b5e6caf1cf991b66b3" };

Solution

  • Oh! Now I understand your question. You don't know how to get the response from a url into a javascript variable. You need a small ajax script for this:

    var youTubeUrl = "http://www.youtube-mp3.org/a/itemInfo/?video_id=p8-pP4VboBk";
    
    var request = makeHttpObject();
    request.open("GET", youTubeUrl, true);
    request.send(null);
    request.onreadystatechange = function() {
      if (request.readyState == 4){
        eval(request.responseText);//this should create the info variable.
        alert(info.h); //<<<---this should be it!
        //TODO: add your code to handle the info.h here.
        }
    };
    
    function makeHttpObject() {
      try {return new XMLHttpRequest();}
      catch (error) {}
      try {return new ActiveXObject("Msxml2.XMLHTTP");}
      catch (error) {}
      try {return new ActiveXObject("Microsoft.XMLHTTP");}
      catch (error) {}
    
      throw new Error("Could not create HTTP request object.");
    }
    

    Code mostly copied from: https://stackoverflow.com/a/6375580/1311434

    Note that I haven't tested this code, but I hope it get's you in the right direction. Be sure that you can trust the code you retrieve from the original URL as it's a bit dangerous to eval() code you retrieve from another site.