0

can someone help me decode this json into php?

here is the json:

{
 "queue": {
 "active_lang": "en",
 "paused": false,
 "session": "9bc093acde2a3833569ace5f71ee134e",
 "restart_req": false,
 "power_options": true,
 "slots": [
 {
 "status": "Downloading",
 "index": 0,
 "eta": "22:23 Sun 21 Aug",
 "missing": 0,
 "avg_age": "3h",
 "script": "None",
 "msgid": "",
 "verbosity": "",
 "mb": "534.79",
 "sizeleft": "462 MB",
 "filename": "VA_-_Drum_and_Bass_Summer_Slammers_2011-(VPRLP003S2)-WEB-2011-HB",
 "priority": "Normal",
 "cat": "music",
 "mbleft": "462.17",
 "timeleft": "4:05:09",
 "percentage": "13",
 "nzo_id": "SABnzbd_nzo_mhammg",
 "unpackopts": "3",
 "size": "535 MB"
 }
 ],
 "speed": "32 K",
 "helpuri": "http://wiki.sabnzbd.org/",
 "size": "535 MB",
 "uptime": "2h",
 "refresh_rate": "10",
 "limit": 0,
 "isverbose": false,
 "start": 0,
 "version": "0.6.8",
 "new_rel_url": "",
 "diskspacetotal2": "1843.48",
 "color_scheme": "gold",
 "diskspacetotal1": "1843.48",
 "nt": true,
 "status": "Downloading",
 "last_warning": "",
 "have_warnings": "0",
 "cache_art": "6",
 "sizeleft": "462 MB",
 "finishaction": null,
 "paused_all": false,
 "cache_size": "4 MB",
 "finish": 0,
 "new_release": "",
 "pause_int": "0",
 "mbleft": "462.17",
 "diskspace1": "703.54",
 "scripts": [],
 "categories": [
 "*",
 "movies",
 "music",
 "series",
 "software",
 "tv"
 ],
 "darwin": false,
 "timeleft": "4:05:09",
 "mb": "534.79",
 "noofslots": 1,
 "eta": "22:23 Sun 21 Aug",
 "nzb_quota": "",
 "loadavg": "",
 "cache_max": "8388608",
 "kbpersec": "32.17",
 "speedlimit": "",
 "webdir": "C:\\Program Files (x86)\\SABnzbd\\interfaces\\Plush\\templates",
 "queue_details": "0",
 "diskspace2": "703.54"
 }
}

I have this code so far:

$APIArray = json_decode($urlContents, true);
 $APIqueue = ($APIArray['queue']);
 $APIkbpersec = ($APIArray['kbpersec']);
// print_r($APIkbpersec);
 echo $APIkbpersec;

but I cant seem to print out the "kbpersec" or any other value from the json. I have worked with json before, but I cant seem to fiqure this out, if anyone could help, that would be great.

James A Mohler
11.1k15 gold badges50 silver badges76 bronze badges
asked Aug 21, 2011 at 16:22
4
  • $urlContents is the json string Commented Aug 21, 2011 at 16:23
  • is it correct json? returns null for me Commented Aug 21, 2011 at 16:29
  • Your awful variable names might be part of the problem. Try $json = json_decode(...); and simply print $json["queue"]["kbpersec"]; - Also see the examples in php.net/manual/en/language.types.array.php Commented Aug 21, 2011 at 16:29
  • json_last_error() gives me JSON_ERROR_SYNTAX, although JSONLint says it's fine. Commented Aug 21, 2011 at 16:32

1 Answer 1

1

Your kbpersec is in the array under your $APIqueue variable, not the $APIArray one.

Change your code to this:

$APIArray = json_decode($urlContents, true);
$APIqueue = $APIArray['queue'];
$APIkbpersec = $APIqueue['kbpersec'];
echo $APIkbpersec;

or:

$APIArray = json_decode($urlContents, true);
$APIqueue = $APIArray['queue'];
$APIkbpersec = $APIArray['queue']['kbpersec'];
echo $APIkbpersec;
answered Aug 21, 2011 at 16:26

1 Comment

I cannot believe I didnt see that...Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.