简体   繁体   中英

How to parse data from link that is using php json_encode

I would like to parse data from a link, then put them into variables. I have tried

window.onload = function() {
    autoRefreshRadio();
};

function autoRefreshRadio(){
    var disabled = "Unavailable";
    var link = '{"url":"http://itspower.net/fullAPI.php"}';
    var obj = JSON.parse(link);
    alert(obj);
}

but it does not seem to alert anything. I'm quite new to Javascript so any help would be appreciated.

obj is a JS object. If you want the link string, you need to reference the property it's stored it, url :

function autoRefreshRadio(){
    var disabled = "Unavailable";
    var link = '{"url":"http://itspower.net/fullAPI.php"}';
    var obj = JSON.parse(link);
    alert(obj.url);
}

Although you're doing a lot of unnecessary legwork here. Just store the object in obj :

function autoRefreshRadio(){
    var disabled = "Unavailable";
    var obj = {"url":"http://itspower.net/fullAPI.php"}
    alert(obj.url);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM