Reputation: 3
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" };
Upvotes: -1
Views: 587
Reputation: 462
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.
Upvotes: 0
Reputation: 462
Json objects are perfectly integrated in javascript.
This means that for a definition:
var myObject = { "name": "Doe", "parents": {"father": "Louis", "mother": "Ophelia"}};
You can simply access the data with following statements:
var myName = myObject.name;
var myFather = myObject.parents.father;
var myMother = myObject.parents.mother;
It really is as simple as that.
As an exact answer to your question, it is indeed info.h
.
UPDATE:
If you mean the h from the info.pf
url, that would be
var h = info.pf.substr(info.pf.indexOf("?h=")+3, 999);
Upvotes: 0
Reputation: 51
The string you have above is a complete JavaScript object, You can think of this as a hash table or an associative key array where "key":"value"
if you want to index into this array or object you simply use it key thus Object['key']
where key can be an int or a string or any object
Upvotes: 0